Example #1
0
File: skyc.c Project: bitursa/maos
/**
   The main(). It parses the command line, setup the parms, ask the scheduler
   for signal to proceed, and then starts skysim to do sky coverage.
 */
int main(int argc, const char *argv[]){
    dirstart=mygetcwd();
    char *scmd=argv2str(argc, argv, " ");
    ARG_S* arg=parse_args(argc,argv);
    /*In detach mode send to background and disable drawing*/
    if(arg->detach){
	daemonize();
    }else{
	redirect();
    }
    info2("%s\n", scmd);
    info2("Output folder is '%s'. %d threads\n",arg->dirout, arg->nthread);
    skyc_version();
    /*register signal handler */
    register_signal_handler(skyc_signal_handler);
    /*
      Ask job scheduler for permission to proceed. If no CPUs are available,
      will block until ones are available.  if arg->force==1, will run
      immediately.
    */
    scheduler_start(scmd,arg->nthread,0,!arg->force);
    /*setting up parameters before asking scheduler to check for any errors. */
    dirsetup=stradd("setup",NULL);
    PARMS_S * parms=setup_parms(arg);
    if(parms->skyc.dbg){
	mymkdir("%s",dirsetup);
    }
    if(!arg->force){
	info2("Waiting start signal from the scheduler ...\n");
	/*Failed to wait. fall back to own checking.*/
	int count=0;
	while(scheduler_wait()&& count<60){
	    warning_time("failed to get reply from scheduler. retry\n");
	    sleep(10);
	    count++;
	    scheduler_start(scmd,arg->nthread,0,!arg->force);
	}
	if(count>=60){
	    warning_time("fall back to own checker\n");
	    wait_cpu(arg->nthread);
	}
    }
    info2("Simulation started at %s in %s.\n",myasctime(),myhostname());
    free(scmd);
    free(arg->dirout);
    free(arg);
    THREAD_POOL_INIT(parms->skyc.nthread);
    /*Loads the main software*/
    OMPTASK_SINGLE skysim(parms);
    free_parms(parms);
    free(dirsetup);
    free(dirstart);
    rename_file(0);
    scheduler_finish(0);
    info2("End:\t%.2f MiB\n",get_job_mem()/1024.);
    info2("Simulation finished at %s in %s.\n",myasctime(),myhostname());
    return 0;
}
Example #2
0
int threaded_subDivide(subDivide_parms *parms)
{
    int i,j,k;
    int ret;

    pthread_t *thread;
    int *thread_ret;
    threaded_subDivide_parms *thread_parms=NULL;
    threaded_subDivide_parms thread_model;
    int nparms=0;
    
   
    alloc_copy_parms(&(thread_model.parms),parms);
    nparms = CreateThreadsParams (&thread_parms,&thread_model);
			 
    printf ("  Started %d threads ...\n",nparms);fflush(0);
    
    thread=calloc(nparms,sizeof(pthread_t));
    thread_ret=calloc(nparms,sizeof(int));

#ifndef USE_OPENMP  
    for (i=0;i<nparms;i++)
    {
	thread_ret[i] = pthread_create( &thread[i], NULL, 
					pthread_subDivide, 
					(void*) (&thread_parms[i]));
    }

    for (i=0;i<nparms;i++)
    {
	pthread_join(thread[i],NULL);
	
    }
#endif
#ifdef USE_OPENMP
#pragma omp parallel for	
	for (i=0;i<nparms;i++)
	{
	    pthread_subDivide((void*) (&thread_parms[i]));
	}
#endif

    for (i=0;i<nparms;i++)
	for (j=0;j<parms->nbins_tot;j++)
	{
	    parms->val[j]+=thread_parms[i].parms.val[j];
	    parms->wei[j]+=thread_parms[i].parms.wei[j];
	    if (parms->nperm)
	    {
		for (k=0;k<parms->nperm;k++)
		{
		    parms->wei_shuffle[k][j]+=thread_parms[i].parms.wei_shuffle[k][j];
		}
	    }
	}

    for (i=0;i<nparms;i++)
    {
	free_parms(&(thread_parms[i].parms));
	free(thread_parms[i].node);
    }

    free(thread_parms);
    free_parms(&(thread_model.parms));
    free(thread);
    free(thread_ret);

    return ret;
}