Пример #1
0
static void
pvm_ferror(const char *string, int p)
{
    if(p)
        pvm_perror((char*)string);
    else
        fprintf(stderr, "%s\n", string);
    pvm_exit();
    exit(1);
}
Пример #2
0
//@cindex waitForPEOp
rtsPacket 
waitForPEOp(OpCode op, GlobalTaskId who, void(*processUnexpected)(rtsPacket) )
{
  rtsPacket p;
  int nbytes;
  OpCode opCode;
  GlobalTaskId sender_id;
  rtsBool match;

  IF_PAR_DEBUG(verbose,
	       fprintf(stderr,"~~ waitForPEOp: expecting op = %x (%s), who = [%x]\n", 
		       op, getOpName(op), who)); 

  do {
    while((p = pvm_recv(ANY_TASK,ANY_OPCODE)) < 0)
      pvm_perror("waitForPEOp: Waiting for PEOp");
      
    pvm_bufinfo( p, &nbytes, &opCode, &sender_id );
    match = (op == ANY_OPCODE || op == opCode) && 
            (who == ANY_TASK || who == sender_id);

    if (match) {
      IF_PAR_DEBUG(verbose,
		   fprintf(stderr,
			   "~~waitForPEOp: Qapla! received: OpCode = %#x (%s), sender_id = [%x]",
			   opCode, getOpName(opCode), sender_id)); 

      return(p);
    }

    /* Handle the unexpected OpCodes */
    if (processUnexpected!=NULL) {
      (*processUnexpected)(p);
    } else {
      IF_PAR_DEBUG(verbose,
		   fprintf(stderr,
			   "~~ waitForPEOp: ignoring OpCode = %#x (%s), sender_id = [%x]",
			   opCode, getOpName(opCode), sender_id)); 
    }

  } while(rtsTrue);
}
Пример #3
0
int StartLog(SeisSlave *node)
{
	char	log_file[256], myhost[256];
	char	*hd;
	FILE *fp;
	
	if ((node -> NodeTid = pvm_mytid()) < 0) {
		pvm_perror("Error enrolling");
		return(-1);
	} 

/* Get host info */
	(void) gethostname(myhost, sizeof(myhost));
	node -> machine = (char *)malloc(strlen(myhost));
	strcpy(node -> machine, myhost);

/* Open the log file */
	if ( !(hd = (char *)getenv("PVMLOG")) ) {
	    hd = "/tmp"; /* Default value */
            fprintf(stderr, "Warning: Variable PVMLOG has not been  set\n");
      	}

	/* creating log files */
	sprintf(log_file, "%s/PSU%s%d\0", hd, myhost, node -> NodeTid);
	/* if ( (node -> fd_log = open(log_file,O_CREAT)) == -1) { */
	if ( (node -> fp_log = freopen(log_file,"w", stderr)) == NULL) {
		fprintf(stderr, "Error opening the log file: %s\n", log_file);
		pvm_exit();
		return(-1);
	}
	
/* Write a header in the log file */
	fprintf(stderr, "%s\n", node -> apl_name);
	fprintf(stderr, 
		"\tFile\t--> %s\n\tapl-tid\t--> %d\n\tMachine\t--> %s \n",
		log_file, node -> NodeTid, node -> machine);
	fflush(stderr);

	return(0);
} /* end of StartLog() */
Пример #4
0
void BBSDirect::perror(const char* s) {
	pvm_perror((char*)s);
}
Пример #5
0
/*---------------------------------------------------------------------------
**  MBUSCONNECT -- Connect the client to the message bus.  In the case of
**  a client we'll look first for a running Supervisor and notify them we've
**  come online.  The Client connection is complete when a MB_READY state
**  is sent.
*/
int
mbusConnect (char *whoAmI, char *group, int singleton)
{
    int     i, mytid, super, info, oldval;
    MBusPtr mbus = (MBusPtr) NULL;
    char    msg[SZ_LINE];
    pid_t   pid = getpid();


    /* Initialize host name.
    */
    mbInitMBHost ();
    mbSetMBHost ();


    /*  At this point we're only getting state information from the
     *  application cache, we don't talk to the message bus yet.
     */
    if (mbAppGet (APP_INIT)) {
	fprintf (stderr, "Application already initialized, resetting....\n");
        mbInitApp();

	/* FIXME:  ....disconnect old client.....*/
    }


    /*if (isSupervisor (whoAmI) ) {*/
        if (MB_DEBUG) printf ("Re-Initializing default message bus...\n");
        mbusInitialize (whoAmI, NULL);

	/*  Attach this process to the message bus.
	 */
    	if ((mytid = pvm_mytid()) < 0) {
	    pvm_perror (whoAmI);
	    exit (1);
    	}
    	oldval = pvm_setopt (PvmAutoErr, 0);  /* disable libpvm msgs */
    /*}*/

    /* No MBus pointer found meaning we have no connection established
     * or context yet.  If we're a SUPERVISOR, reinitialize.  Otherwise,
     * now is the time to connect and set the state.
     */
    mbus = mbAppGetMBus ();
    if (mbus == (MBusPtr) NULL) {
	/*  Message bus not initialized by supervisor.
    	 *  Attach this process to the message bus.
	 */
    	if ((mytid = pvm_mytid()) < 0) {
	    pvm_perror (whoAmI);
	    exit (1);
    	}
    	oldval = pvm_setopt (PvmAutoErr, 0);  /* disable libpvm msgs */

        /* Allocate an message bus structure.  We'll update the state from
	 * the supervisor once connected and setup.
         */
        if (MB_DEBUG) printf ("Allocating mbus struct ...\n");
        mbus = (MBusPtr) calloc (1, sizeof (MBus));
    }




    /**********************
     *  CLIENT CODE
     **********************/
    if (! isSupervisor (whoAmI)) {
        /*  If we're a Client, look for a Supervisor.  If we can't find a
         *  Supervisor the simply return an error, individuals client may 
         *  handle it differently.
         */
	for (i=MB_CONNECT_RETRYS; i; i--) {
            info = pvm_lookup (SUPERVISOR, -1, &super);
	    if (info == PvmNoEntry) {
	        fprintf (stderr, "Can't find Supervisor on msgbus, retrying\n");
		sleep (1);

	    } else {
	        if (MB_DEBUG) {
		    printf ("Supervisor on msgbus at tid=%d\n", super);
                    printf ("Registering '%s' with VM....\n", whoAmI);
	        }

                if (pvm_insert (whoAmI, -1, mytid) < 0) {
                    fprintf (stderr, "Register of '%s' failed....\n", whoAmI);
                    return (ERR);

                } else {
		    /* Send the Supervisor a CONNECT message. 
		     */
		    memset (msg, 0, SZ_LINE);
		    sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }",
			mytid, whoAmI, mbGetMBHost(), pid);
		    mbusSend (SUPERVISOR, ANY, MB_CONNECT, msg);
		}
		break;
	    }
	}
	if (i == 0) {
	    if (MB_VERBOSE)
	        fprintf (stderr, "Supervisor not on msgbus, returning\n");
	    mytid *= (-1);
	    super = ERR;
	    /*return (((-1) * mytid)); */
	}


    /*********************
     * SUPERVISOR CODE
     **********************/
    } else if (isSupervisor (whoAmI)) {
	/* If we're a Supervisor, check whether we're already registered.
	 */
        info = pvm_lookup (SUPERVISOR, -1, &super);
	if (info == PvmBadParam) {
	    fprintf (stderr, "Supervisor lookup error, BadParam...\n");
	    return (ERR);

	} else if (info == PvmNoEntry) {
            /*  Supervisor does not exist */
	    if (MB_DEBUG) 
		fprintf (stderr, "Supervisor not found, registering...\n");
            if (pvm_insert (SUPERVISOR, -1, (super = mytid)) < 0) {
                fprintf (stderr, "Supervisor register failed....\n");
                return (ERR);
            }

	} else if (info >= 0) {
	    if (MB_DEBUG)
		fprintf (stderr, 
		    "Supervisor already registered at tid=%d, validating..\n",
		    super);

	    /* Try sending a message to the registered tid to see if it is
	     * alive.
	     */
	    if (mbusPing (super, 500) == OK) {
		if (singleton) {
		    /* tell other supervisor we're taking over.... */
		    printf ("got a ping reply....\n");
		} else {
		    fprintf (stderr, 
		        "ERROR: Supervisor already registered at tid=%d\n",
			super);
	            return (ERR);
		}

	    } else {
		/* The registered supervisor didn't respond, so delete it
		 * from the database and try again.
		 */
		extern int pvmreset();

	        if (MB_DEBUG)
		    fprintf (stderr, "Cleaning up earlier super at %d(%d)...\n",
			info, super);

		/* FIXME:  ....disconnect old supervisor.....*/
		(void) pvmreset (mytid, 1, "", 0);
    		if (pvm_delete (SUPERVISOR, info) != PvmOk)
		    if (MB_DEBUG)
        	        fprintf (stderr, "Supervisor cleanup failed\n");
    		if ((info = pvm_kill (super)) != PvmOk)
		    if (MB_DEBUG)
        	        fprintf (stderr, "Supervisor assasination failed\n");
		/* goto lookup_; */
	    }
        }


	/* Broadcast a message to any running clients telling them we're
	 * now running.
	 */
	memset (msg, 0, SZ_LINE); 
	sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }", 
	    super, SUPERVISOR, mbGetMBHost(), pid);

	mbusBcast (CLIENT, msg, MB_CONNECT);
    }

    /* Save the Supervisor location and other bits about this client.
     */
    mbAppSet (APP_TID, mytid);
    mbAppSet (APP_STID, super);
    mbAppSet (APP_FD, mbGetMBusFD());
    mbAppSetName (whoAmI);
    mbAppSetMBus ((MBusPtr) mbus);


    /* Install the exit handler so we're sure to make a clean getaway.
     */
    atexit (mbusExitHandler);
	        
    if (MB_DEBUG)
	fprintf (stderr, "mbConnect: whoAmI='%s' group='%s'  mbus = 0x%x\n", 
	    whoAmI, group, (int) mbus);


    /* Join a specific group if specified.  A process may be part of
     * multiple groups and so we use the mbus routine the same as a
     * caller would.
     */
    if (group)
	mbusJoinGroup (group);


    /*  Return our tid.
     */
    return (mytid);
}
Пример #6
0
void gradient(float *grad)
{
   /* declaration of variables */
   int i, iF, iR, iProc, iDer, iL, iU, offset; 
                                   /* counters */
   int FReceived;                  /* number of frequencies processed */
   int die;                        /* die processor flag */ 
   int apl_pid;                    /* PVM process id control */
   int pid;                        /* process id */
   int masterId;                   /* master id */
   int processControl;             /* monitoring PVM start */
   int FInfo[2];                   /* frequency delimiters */
   float wallcpu;                   /* wall clock time */     
   float *gradPart;                 /* partition of gradients */
   complex **resCDPart;             /* partition of resCD */
   
   /* Clean up log files */
   CleanLog();
     
   /* Reseting synchronization flags */
   for (i = 0; i < nFreqPart; i++)
   {
      statusFreq[i][2] = 0;
   }
      
   /* allocating some memory */
   gradPart = alloc1float(numberPar * limRange);
    
   for (i = 0; i < numberPar * limRange; i++)
   {
      grad[i] = 0;
   }
   
   fprintf(stderr, "Starting communication with PVM for derivatives\n");
   /* starting communication with PVM */
   if ((apl_pid = pvm_mytid()) < 0)
   {
      pvm_perror("Error enrolling master process");
      exit(-1);
   }
   processControl = CreateSlaves(processes, PROCESS_FRECHET, nProc);
   
   if (processControl != nProc)
   {
      fprintf(stderr,"Problem starting PVM daemons\n");
      exit(-1);
   }
      
   /* converting to velocities */
   if (IMPEDANCE)
   {
      for (i = 0; i < info->nL + 1; i++)
      {
         alpha[i] /= rho[i];
         beta[i] /= rho[i];
      }
   }

   /* Broadcasting all processes common information */
   BroadINFO(info, 1, processes, nProc, GENERAL_INFORMATION);
   
   /* sending all profiles */
   BroadFloat(thick, info->nL + 1, processes, nProc, THICKNESS);
   BroadFloat(rho, info->nL + 1, processes, nProc, DENSITY);
   BroadFloat(alpha, info->nL + 1, processes, nProc, ALPHAS);
   BroadFloat(qP, info->nL + 1, processes, nProc, QALPHA);
   BroadFloat(beta, info->nL + 1, processes, nProc, BETAS);
   BroadFloat(qS, info->nL + 1, processes, nProc, QBETA);

   /* sending frequency partitions for each process */
   for (iProc = 0; iProc < nProc; iProc++)
   {
      FInfo[0] = statusFreq[iProc][0];
      FInfo[1] = statusFreq[iProc][1];
      if (info->verbose)
	 fprintf(stderr, 
	 "Master sending frequencies [%d, %d] out of %d to slave Frechet %d [id:%d]\n", FInfo[0], FInfo[1], info->nF, iProc, processes[iProc]);

      procInfo[iProc][0] = FInfo[0];
      procInfo[iProc][1] = FInfo[1];
      SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
      statusFreq[iProc][2] = 1;
      
      /* and sending the appropriate correlation chunk */
      /* allocating some memory */
      resCDPart = alloc2complex(FInfo[1] - FInfo[0] + 1, info->nR);

      for (iR = 0; iR < info->nR; iR++)
      {
	 for (i = 0, iF = FInfo[0]; iF <= FInfo[1]; iF++, i++)
	 {
	    resCDPart[iR][i] = resCD[iR][iF - initF];
/*	    fprintf(stderr, "iR %d iF %d [%f %f]\n",
		    iR, iF, resCDPart[iR][i].r, resCDPart[iR][i].i);*/
	 }
      }
      
      /* sending frequency partition to the slave process */
      SendCplx(resCDPart[0], (FInfo[1] - FInfo[0] + 1) * info->nR, 
	       processes[iProc], COVARIANCE_PARTITION);
      free2complex(resCDPart);
   }
   /* waiting modelled frequencies */
   /* master process will send more frequencies if there's more work to do */
   /* measuring elapsed time */
   wallcpu = walltime();


   /* reseting frequency counter */
   FReceived = 0;
   while (FOREVER)
   {
      pid = RecvFloat(gradPart, info->numberPar * info->limRange, -1,
		     PARTIAL_GRADIENT);

      /* finding the frequency limits of this process */
      /* DD 
 fprintf(stderr, "Master finding the frequency limits of this process\n");
      */

      iProc = 0;
      while (pid != processes[iProc])
	 iProc++;
	                       
      /* stacking gradient */
      for (i = 0; i < info->numberPar * info->limRange; i++)
      {
	 grad[i] += gradPart[i];
	 /* DD
	 fprintf(stderr, "i %d grad %f gradPart %f\n", i, grad[i], gradPart[i]);*/
      }
      
      /* summing frequencies that are done */
      FReceived += procInfo[iProc][1] - procInfo[iProc][0] + 1;
      if (info->verbose)
	 fprintf(stderr, "Master received %d frequencies, remaining %d\n",
		 FReceived, info->nF - FReceived);
             
      /* defining new frequency limits */
      i = 0;
      while (i < nFreqPart && statusFreq[i][2])
	 i++;

      /* DD 
      fprintf(stderr, "i %d nFreqPart %d\n", i, nFreqPart);*/
           
      if (i < nFreqPart)
      {
	 /* there is still more work to be done */
	 /* tell this process to not die */
	 die = 0;
	 SendInt(&die, 1, processes[iProc], DIE);
	 FInfo[0] = statusFreq[i][0];
	 FInfo[1] = statusFreq[i][1];
	 
	 if (info->verbose)
	    fprintf(stderr, 
		    "Master sending frequencies [%d, %d] to slave %d\n",
		    FInfo[0], FInfo[1], processes[iProc]);

	 procInfo[iProc][0] = FInfo[0];
	 procInfo[iProc][1] = FInfo[1];
	 SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
	 statusFreq[i][2] = 1;

	 /* sending covariance partition */
	 /* allocating some memory */
	 resCDPart = alloc2complex(FInfo[1] - FInfo[0] + 1, info->nR);

	 for (iR = 0; iR < info->nR; iR++)
	 {
	    for (i = 0, iF = FInfo[0]; iF <= FInfo[1]; iF++, i++)
	    {
	       resCDPart[iR][i] = resCD[iR][iF - initF];
	    }
	 }
	 /* sending frequency partition to the slave process */
	 SendCplx(resCDPart[0], (FInfo[1] - FInfo[0] + 1) * info->nR, 
		  processes[iProc], COVARIANCE_PARTITION);
	 free2complex(resCDPart);
      }
      else
      {
	 /* tell this process to die since there is no more work to do */
	 if (info->verbose)
	    fprintf(stderr, "Master ''killing'' slave %d\n", processes[iProc]);
	 die = 1;
	 SendInt(&die, 1, processes[iProc], DIE);
      }
      /* a check to get out the loop */
      if (FReceived >= info->nF) break;
   }

   /* getting elapsed time */
   wallcpu = walltime() - wallcpu;
   fprintf(stderr, "Frechet derivative wall clock time = %f seconds\n\n", 
	   wallcpu);   
   
   /* back to impedances*/
   if (IMPEDANCE)
   {
      for (i = 0; i < info->nL + 1; i++)
      {
         alpha[i] *= rho[i];
         beta[i] *= rho[i];
      }
   }

   /* finally the gradient, the 2 is due Parseval */
   for (iDer = 0; iDer < numberPar * limRange; iDer++)
   {
      grad[iDer] *= 2 / (float) (nTotalSamples * oFNorm);
   }

   /* getting gradient in impedance domain */
   if (IMPEDANCE)
   {
      offset = 0;
      for (i = lim[0], iL = 0; iL < limRange; iL++, i++)
      {
         if (vpFrechet) 
         {
            grad[iL] /= rho[i];
            offset = limRange;
	 }
	 
         if (vsFrechet) 
         {
            grad[iL + offset] /= rho[i];
            offset += limRange;
	 }
	 
         if (rhoFrechet)
         {
            grad[iL + offset] = - alpha[i] * grad[iL] -
	      beta[i] * grad[iL + limRange] + grad[iL + 2 * limRange];
         }
      }
   }

   if (PRIOR)
   {
      auxm1 = 1. / (float) (numberPar * limRange);     /* normalization */
      /* considering the regularization or model covariance term */
      for (i = 0; i < limRange; i++)
      {
	 for (offset = i, iL = 0; iL < limRange; iL++)
	 {
	    iU = 0;
	    if (vpFrechet)
	    {
	       grad[iL] += (alpha[i + lim[0]] - alphaMean[i + lim[0]]) * 
		            CMvP[offset] * auxm1;
	       iU = limRange; /* used as offset in gradient vector */
	    }
	    
	    if (vsFrechet)
	    {
	       grad[iL + iU] += (beta[i + lim[0]] - betaMean[i + lim[0]]) * 
	 	                 CMvS[offset] * auxm1;
	       iU += limRange;
	    }

	    if (rhoFrechet)
	    {
	       grad[iL + iU] += (rho[i + lim[0]] - rhoMean[i + lim[0]]) * 
		                 CMrho[offset] * auxm1;
	    }

	    offset += MAX(SGN0(i - iL) * (limRange - 1 - iL), 1);
	 }
      }
   }

   /* normalizing gradient 
   normalize(grad, numberPar * limRange);*/
   /* freeing memory */
   free1float(gradPart);
}
Пример #7
0
/// user function:add a new query to a pipeline of filters. Called by user manager runs this.
///	\param layout System Layout.
///	\param work Buffer with a Unit of Work (UoW)
///	\param workSize Unit of Work Size (UoW)
///	\return Zero on success, -1 on error.
int appendWork(Layout *layout, void *work, int workSize) {
#ifdef NO_BARRIER

    // sends work for each filter
    pvm_initsend(PvmDataRaw);
    // First tell that is a mensage of WORK
    int msgType = MSGT_WORK;
    pvm_pkint(&msgType, 1, 1);
    //then attach the work to it
    pvm_pkbyte((char *)work, workSize, 1);

    // for each filter, send his work
    for(i = 0; i < layout->numFilters; i++) {
        FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement);
        // sends work to all filters of this set
        pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0);
    }

#else
    int i;
    int totalEows = 0, numEowsReceived;
#ifdef ATTACH
    int totalAttachedFilters = 0;
#endif
    int reconf = 0 /** should we reconfigure? */, remainingReconfs = 0; //how many times should we try?

#ifdef VOID_INST
#ifdef BMI_FT
    char instDir[MAX_IDIR_LENGTH];
    sprintf(instDir, "%s/", INST_DIR);
    for (i=0; i < layout->numFilters-1; i++) {
        if (strlen(instDir) >= (MAX_IDIR_LENGTH - 3)) {
            //dont want to overflow this array
            fprintf(stderr, "%s %d: warning, instrumentation directory name too big, truncating to %s\n", __FILE__, __LINE__, instDir);
            break;
        }
        sprintf(instDir, "%s%d-", instDir, layout->filters[i]->filterPlacement.numInstances);
    }
    sprintf(instDir, "%s%d", instDir, layout->filters[layout->numFilters-1]->filterPlacement.numInstances);
    char managerLogFile[MAX_IDIR_LENGTH+20];
    sprintf(managerLogFile, "%s/manager.time", instDir);
    FILE *fp = fopen(managerLogFile, "w");

    struct timeval tv;
    struct timezone tz; //not used
    //get the time
    gettimeofday(&tv, &tz);

    if(fp != NULL)
        fprintf(fp, "1 %ld %ld\n", tv.tv_sec, tv.tv_usec);
#endif
#endif

    //before sending, we check if we received any filter error
    int bufid = pvm_probe(-1, MSGT_FERROR);
    if (bufid != 0) {
        int bytes, tag, tid;
        char *msg;
        pvm_bufinfo(bufid, &bytes, &tag, &tid);
        msg = (char*)malloc(bytes+1);
        pvm_recv(tid, MSGT_FERROR);
        pvm_upkbyte(msg, bytes, 1);
        msg[bytes] = '\0';

        fprintf(stderr, "Manager.c: Error, received death notification\n");
        fprintf(stderr, "Manager.c: %s\n", msg);
        free(msg);
        killAllFilters(layout);
        exit(-1);
    }

    printf("Manager.c: starting work...\n");

    // number of EOWs we expect to receive
    for(i = 0; i < layout->numFilters; i++) {
        totalEows += layout->filters[i]->filterPlacement.numInstances;
#ifdef ATTACH
        if(layout->filters[i]->attached) {
            totalAttachedFilters += layout->filters[i]->filterPlacement.numInstances;
        }
        // TODO:TOSCO:::
        if(layout->filters[i]->attach) return 0;
#endif
    }

    //we stay in this loop while we have to reconfigure
    //usually, this will be only one time, unless a we get some reconf message
    do {

        // sends work for each filter
        pvm_initsend(PvmDataRaw);
        int msgType;
#ifdef VOID_FT
        if(!reconf) {
#endif
            // First tell that is a mensage of WORK
            msgType = MSGT_WORK;
#ifdef VOID_FT
        } else {
            // one fault has occurred
            msgType = MSGT_FT;
        }
#endif
        pvm_pkint(&msgType, 1, 1);
        //then attach the work to it
        pvm_pkbyte((char *)work, workSize, 1);

        reconf = 0; //we are optimistic, always expect to not reconfigure


        // for each filter, send his work
        for(i = 0; i < layout->numFilters; i++) {
            FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement);
            // sends work to all filters of this set
            pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0);
        }
        /*

        		TaskIdList *finalTaskIdList = NULL, *currentTaskIdList;
        		int filtersThatUseTasks = 0;
        		// Manager receives filter's terminated tasks list
        		for(i = 0; i < layout->numFilters; i++) {
        			FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement);
#ifdef ATTACH
        			// if this filter is of the attached
        			// type i dont need worry about use Task
        //			if(layout->filters[i]->attached)continue;
#endif
        			for(j = 0; j < pFilterP->numInstances; j++) {
        				int instanceUseTasks = -1;

        				// Get is this filter use tasks
        				pvm_recv(pFilterP->tids[j], 0);
        				pvm_upkint(&instanceUseTasks, 1, 1);
        				layout->filters[i]->useTasks = instanceUseTasks;

#ifdef VOID_FT
        				if (instanceUseTasks) {
        					currentTaskIdList = (TaskIdList *)unpackTaskIdList();

        					//	Para fazer intersecao, gerente ordenar? as listas de tarefas recebidas e utilizar? a fun??o meet() do CrazyMiner/ID3.
        					qsort(currentTaskIdList->vetor, currentTaskIdList->size, sizeof(int), compareTaskId);
        					if(finalTaskIdList == NULL) {
        						finalTaskIdList = currentTaskIdList;
        					} else {
        						// 	Manager makes the intersection of all finished tasks lists
        						finalTaskIdList = taskIdListIntersection(finalTaskIdList, currentTaskIdList);
        						taskIdListDestroy(currentTaskIdList);
        					}
        				}
#endif

        			} // for

        			if (layout->filters[i]->useTasks) filtersThatUseTasks++;
        		}

        		//	Gerente devolve resultado das intersecoes para todas as instancias de todos os filtros.
        		for(i = 0; i < layout->numFilters; i++) {
         			FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement);
#ifdef ATTACH
        			// if this filter is of the attached
        			// type i dont need worry about use Task
        //TODO			if(layout->filters[i]->attached)continue;
#endif

        			int needForwardTaskMessages = 1;
        			if (filtersThatUseTasks < 2) needForwardTaskMessages = 0;

#ifdef VOID_FT
        			if (layout->filters[i]->useTasks) {
        				// Send if they should forward task messages
        				// and pigback :-) the final task id list
         				pvm_initsend(PvmDataDefault);
         				pvm_pkint(&needForwardTaskMessages, 1, 1);

        				packTaskIdList(finalTaskIdList);
        				pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0);
        			} else {
#endif
        				// Only send if they should forward task messages
         				pvm_initsend(PvmDataDefault);
         				pvm_pkint(&needForwardTaskMessages, 1, 1);
        				pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0);
#ifdef VOID_FT
        			}
#endif
        		}
        		taskIdListDestroy(finalTaskIdList);

        */

        //now we receive the EOWs
        numEowsReceived = 0;

        //now we expect to receive EOW or errors
        while(numEowsReceived < totalEows) {
            //we are open to receive anything from anyone here
            //all messages to the manager should be tagged, so we now their type
            int szMsg = -1;
            int inst_tid = -1;
            int msgTag = -1;
            int bufid = pvm_recv(-1, -1);
            pvm_bufinfo(bufid, &szMsg, &msgTag, &inst_tid);

            switch (msgTag) {
            case (MSGT_EOW): {
                //received EOW, expect this usually
                int instance = -1;
                FilterSpec *pFilter = NULL;

                getFilterByTid(layout, inst_tid, &pFilter, &instance);

                if ((pFilter != NULL) && (instance != -1)) {
                    printf("Manager.c: EOW received from %s, instance %d\n",
                           pFilter->name, instance);
                } else {
                    fprintf(stderr, "Manager.c: unknown EOW received! Shouldnt get here!\n");
                }
                numEowsReceived++;
                break;
            }
            case (MSGT_AEXIT):
            case (MSGT_FERROR): {
                //someone called dsExit or system error at the filter side
                //common cause for this are library not found, wrong initscritpt etc
                char *message = (char*)malloc(sizeof(char)*szMsg+1);
                pvm_upkbyte(message, szMsg, 1);
                message[szMsg] = '\0';

                //the filter and the instance
                FilterSpec *fp = NULL;
                int instance = -1;
                getFilterByTid(layout, inst_tid, &fp, &instance);

                if (msgTag == MSGT_AEXIT) {
                    printf("Manager.c: Filter %s, instance %d(tid %x) called dsExit: %s\n",
                           fp->name, instance, inst_tid, message);
                } else {
                    printf("Manager.c: Filter %s error, instance %d(tid %x) called exit: %s\n",
                           fp->name, instance, inst_tid, message);
                }
                free(message);
                // kill all instances
                killAllFilters(layout);
                exit(-1);
                break;
            }
            //task exited or host crashed
            case (MSGT_TEXIT):
            case (MSGT_HDEL): {
                //we only reconfigure a fixed number of times
                if (remainingReconfs <= 0) {
                    //max number of reconfigurations reached... aborting
                    fprintf(stderr, "Manager.c: max reconfigurations reached, aborting...\n");
                    fflush(stderr);
                    fprintf(stdout, "Manager.c: max reconfigurations reached, aborting...\n");
                    fflush(stdout);
                    reconf = 0;

                    // kill all instances which might be alive
                    killAllFilters(layout);
                    exit(-1);;

                }
#ifdef BMI_FT
                gettimeofday(&tv, &tz);
                fprintf(fp, "2 %ld %ld\n", tv.tv_sec, tv.tv_usec);
#endif

                remainingReconfs--;
                reconf = 1;
                // In case of pvm notification, inst_tid will be t80000000
                int notifiesRecv = 1; // We are receiving the first death notification
                int deadFilterTid = -1;
                FilterSpec *pFilter = NULL;
                int instanceDead = -1;

                // Get the tid and name of the dead filter
                int info = pvm_upkint(&deadFilterTid, 1, 1);
                if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint");

                //discover which filter died
                getFilterByTid(layout, deadFilterTid, &pFilter, &instanceDead);

                if((pFilter != NULL) && (instanceDead != -1)) {
                    if (msgTag == MSGT_TEXIT) {
                        fprintf(stderr, "Manager.c: filter %s: instance %d (tid t%x) of %d is dead!!!\n",
                                pFilter->name, instanceDead, deadFilterTid, pFilter->filterPlacement.numInstances);
                    } else {
                        fprintf(stderr, "Manager.c: filter %s: instance %d (tid t%x) of %d machine's crashed!!!\n",
                                pFilter->name, instanceDead, deadFilterTid, pFilter->filterPlacement.numInstances);
                    }
                }
                printf("Manager.c: starting reconfiguration\n");

                // kill all filters in the pipeline
                killAllFilters(layout);

                if (msgTag == MSGT_HDEL) {
                    //int his case, host died, so we must change layout
                    replaceCrashedHost(layout, pFilter, instanceDead);
                }

#ifdef ATTACH
                if (pFilter->attached) {
                    // In this case, all filters that were killed have to notify
                    // their dead.
                    notifiesRecv = 0;
                }
#endif

                //Flush the streams
                //receive all messages which are about to arrive till we get the death notification
                //pvm order should garantee this
#ifdef ATTACH
                while (notifiesRecv < (totalEows - totalAttachedFilters)) {
#else
                while (notifiesRecv < totalEows) {
#endif
                    int newMsgTag = -1;
                    bufid = pvm_recv(-1, MSGT_TEXIT);
                    info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid);
                    info = pvm_upkint(&deadFilterTid, 1, 1);
                    if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint");

                    fprintf(stderr, "Manager.c: WARNING: received notification (tag %d) about pvm tid t%x death\n", newMsgTag, deadFilterTid);
                    notifiesRecv++;
                }

#ifdef ATTACH
                if(pFilter->attached) {
                    notifiesRecv = 1;
                } else {
                    notifiesRecv = 0;
                }
                // Receive all EOW messages from the attached filters.
                while(notifiesRecv < totalAttachedFilters) {
                    int newMsgTag = -1;
                    bufid = pvm_recv(-1, MSGT_EOW);
                    info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid);
                    if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint");

                    fprintf(stderr, "Manager.c: WARNING: received EOW (tag %d) from pvm tid t%x\n", newMsgTag, inst_tid);
                    notifiesRecv++;
                }
#endif
                // probes for remaining machine crash notifications
                while (pvm_probe(-1, MSGT_HDEL) > 0) {
                    int newMsgTag = -1;
                    bufid = pvm_recv(-1, MSGT_HDEL);
                    info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid);
                    info = pvm_upkint(&deadFilterTid, 1, 1);
                    if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint");

                    fprintf(stderr, "Manager.c: WARNING: received notification (tag %d) about pvm tid t%x machine's crash\n", newMsgTag, deadFilterTid);

                    // Replace the died host
                    FilterSpec *pCrashedFilter = NULL;
                    int crashedInstance = -1;
                    getFilterByTid(layout, deadFilterTid, &pCrashedFilter, &crashedInstance);
                    replaceCrashedHost(layout, pCrashedFilter, crashedInstance);
                }
#ifdef BMI_FT
                updateAllFiltersFaultStatus(layout, FAULT_OTHER_FILTER_INST);
                pFilter->faultStatus = instanceDead;
#endif

                //spawn all filters again
                spawnAllFilter(layout);
#ifdef ATTACH
                // Verifies if the dead filter is an attached. If yes, spawn it.
                if(pFilter->attached == 1) {
                    spawnOneAttachedInstance(layout, pFilter, instanceDead);
                }
#endif
                resetStreams(layout);
                //resend the data
                sendFiltersData(layout);
                //start all over again
                numEowsReceived = 0;

#ifdef BMI_FT
                gettimeofday(&tv, &tz);
                fprintf(fp, "3 %ld %ld\n", tv.tv_sec, tv.tv_usec);
#endif
                break;
            }
#ifdef VOID_TERM
        // One filter instance detected local termination
            case (MSGT_LOCALTERM): {
                int localTermTag; // filter instance local termination tag
                pvm_upkint(&localTermTag, 1, 1);
                verifyGlobalTermination(inst_tid, localTermTag);
                break;
            }
#endif
            default: {
                fprintf(stderr, "Manager.c: error receiving EOW, unknown tag!!!\n");
            }
        } //end switch message tag
        if((msgTag == MSGT_TEXIT) || (msgTag == MSGT_HDEL)) {
            // work should be sent again
            break;
        }
        } //end receiving eows
    }
    while(reconf == 1); //leave this loop if we will not reconfigure

#ifdef BMI_FT
    gettimeofday(&tv, &tz);

    fprintf(fp, "4 %ld %ld\n", tv.tv_sec, tv.tv_usec);
#endif

    printf("Manager.c: Work ended\n\n");
    return 0;
#endif
}


/// Finalize a Void pipeline. Only manager runs this.
int finalizeDs(Layout *layout) {
#ifdef NO_BARRIER

#else
    int i;

    // Envia eof para todos os filtros
    // Primeiro envia se eh work (WORK) ou EOF (END_OF_FILTER)
    pvm_initsend(PvmDataRaw);
    int tipo_msg = MSGT_EOF;
    pvm_pkint(&tipo_msg, 1, 1);
    //sends the EOF message for all instances of the filter
    for(i = 0; i < layout->numFilters; i++) {
        FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement);
#ifdef ATTACH
        if(layout->filters[i]->attach) continue;// this filter cant not receive a EOF because
        // it needs still runnig
#endif
        pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0);
    }
    destroyLayout(layout);
    pvm_exit();
    return 0;
#endif
}
Пример #8
0
float modeling()
{
   /* declaration of variables */
   FILE *fp;                       /* to report results */
   int iF, iF1, iR, offset, iT1, iT2, iS, iProc, i, k;
                                   /* counters */
   int wL;                         /* window length */
   int die;                        /* die processor flag */
   int FReceived;                  /* number of frequencies processed */
   int apl_pid;                    /* PVM process id control */
   int pid;                        /* process id */
   int processControl;             /* monitoring PVM start */
   int FInfo[2];                   /* frequency delimiters */
   float wallcpu;                  /* wall clock time */
   float oF;                       /* value of the objective function */
   float residue;                  /* data residue */
   float wdw;                      /* windowing purposes */
   float *buffer, *bufferRCD;      /* auxiliary buffers */
                                   /* upgoing waves */
   complex **dataS;                /* synthethics in the frequency domain */
   complex *bufferC;               /* auxiliary buffer */
   complex **freqPart;             /* frequency arrays sent by the slaves */
   
   /* Clean up log files */
   CleanLog();

   /* Reseting synchronization flags */
   for (i = 0; i < nFreqPart; i++)
   {
      statusFreq[i][2] = 0;
   }
    
   /* allocating some memory */
   dataS = alloc2complex(info->nF, info->nR);
   buffer = alloc1float(info->nSamples);
   bufferRCD = alloc1float(info->nSamples);
   bufferC = alloc1complex(info->nSamples / 2 + 1);
   freqPart = alloc2complex(info->nFreqProc, info->nR);

   /* reseting */
   for (iF = 0; iF < info->nSamples / 2 + 1; iF++)
      bufferC[iF] = zeroC;
   for (iS = 0; iS < info->nSamples; iS++)
   {
      buffer[iS] = 0; bufferRCD[iS] = 0;
   }

   /* DD 
   fprintf(stderr, "nF -> %d\n", info->nF);*/
   fprintf(stderr, "Starting communication with PVM for modeling\n");

   /* starting communication with PVM */
   if ((apl_pid = pvm_mytid()) < 0) 
   {
      pvm_perror("Error enrolling master process");
      exit(-1);
   }
   processControl = CreateSlaves(processes, PROCESS_MODELING, nProc);
   
   if (processControl != nProc)
   {
      fprintf(stderr,"Problem starting PVM daemons\n");
      exit(-1);
   }

   /* converting to velocities */
   if (IMPEDANCE)
   {
      for (i = 0; i < info->nL + 1; i++)
      {
         alpha[i] /= rho[i];
         beta[i] /= rho[i];
      }
   }
   
   /* Broadcasting all processes common information */
   BroadINFO(info, 1, processes, nProc, GENERAL_INFORMATION);
   
   /* sending all profiles */
   BroadFloat(thick, info->nL + 1, processes, nProc, THICKNESS);
   BroadFloat(rho, info->nL + 1, processes, nProc, DENSITY);
   BroadFloat(alpha, info->nL + 1, processes, nProc, ALPHAS);
   BroadFloat(qP, info->nL + 1, processes, nProc, QALPHA);
   BroadFloat(beta, info->nL + 1, processes, nProc, BETAS);
   BroadFloat(qS, info->nL + 1, processes, nProc, QBETA);
   
   /* sending frequency partitions for each process */
   for (iProc = 0; iProc < nProc; iProc++)
   {
      FInfo[0] = statusFreq[iProc][0];
      FInfo[1] = statusFreq[iProc][1];

      if (info->verbose)
	 fprintf(stderr, 
		 "Master sending frequencies [%d, %d] out of %d to slave Modeling %d [id:%d]\n", FInfo[0], FInfo[1], info->nF, iProc, processes[iProc]);
      
      procInfo[iProc][0] = FInfo[0];
      procInfo[iProc][1] = FInfo[1];
      SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
      statusFreq[iProc][2] = 1;
   }

   /* waiting modelled frequencies */
   /* master process will send more frequencies if there's more work to do */
   /* measuring elapsed time */
   wallcpu = walltime();

   /* reseting frequency counter */
   FReceived = 0;

   while (FOREVER)
   {
      pid = RecvCplx(freqPart[0], info->nR * info->nFreqProc, -1, 
		     FREQUENCY_PARTITION);

      /* finding the frequency limits of this process */
      /* DD 
      fprintf(stderr, "Master finding the frequency limits of this process\n");
      */

      iProc = 0;
      while (pid != processes[iProc])
	 iProc++;

      /* DD 
      fprintf(stderr, "iProc %d pid %d\n", iProc, pid);*/

      /* copying into proper place of the total frequency array */
      for (iR = 0; iR < info->nR; iR++)
      {
	 for (k = 0, i = procInfo[iProc][0]; i <= procInfo[iProc][1]; i++, k++)
	 {
	    dataS[iR][i - initF] = freqPart[iR][k];
	 }
      }
      
      /* summing frequencies that are done */
      FReceived += procInfo[iProc][1] - procInfo[iProc][0] + 1;

      if (info->verbose)
	 fprintf(stderr, "Master received %d frequencies, remaining %d\n", 
		 FReceived, info->nF - FReceived);

      /* defining new frequency limits */
      i = 0;
      while (i < nFreqPart && statusFreq[i][2])
	 i++;

      /* DD 
      fprintf(stderr, "i %d nFreqPart %d\n", i, nFreqPart);*/
      
      if (i < nFreqPart)
      {
	 /* there is still more work to be done */
	 /* tell this process to not die */
	 die = 0;
	 SendInt(&die, 1, processes[iProc], DIE);
	 FInfo[0] = statusFreq[i][0];
	 FInfo[1] = statusFreq[i][1];

	 if (info->verbose)
	    fprintf(stderr, "Master sending frequencies [%d, %d] to slave %d\n", FInfo[0], FInfo[1], processes[iProc]);

	 procInfo[iProc][0] = FInfo[0];
	 procInfo[iProc][1] = FInfo[1];
	 SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
	 statusFreq[i][2] = 1;
      }
      else
      {
	 /* tell this process to die since there is no more work to do */
	 if (info->verbose)
	    fprintf(stderr, "Master ''killing'' slave %d\n", processes[iProc]);
	 die = 1;
	 SendInt(&die, 1, processes[iProc], DIE);
      }

      /* a check to get out the loop */
      if (FReceived >= info->nF) break;
   }
   
   /* quitting PVM */
   EndOfMaster();
   
   /* getting elapsed time */
   wallcpu = walltime() - wallcpu;
   fprintf(stderr, "Modeling wall clock time = %f seconds\n", 
	   wallcpu);
     
   /* back to impedances*/
   if (IMPEDANCE)
   {
      for (i = 0; i < info->nL + 1; i++)
      {
         alpha[i] *= rho[i];
         beta[i] *= rho[i];
      }
   }

   /* computing the objective function for the time window */
   for (oF = 0, residue = 0, iR = 0; iR < info->nR; iR++)
   {
      /* windowing as it was done to the input data */
      iT1 = NINT(info->f1 / info->dF);
      iT2 = NINT(info->f2 / info->dF);
      wL = info->nF * PERC_WINDOW / 2;
      wL = 2 * wL + 1;
      for (iS = 0, iF = 0; iF < info->nSamples / 2 + 1; iF++)
      {
	 if (iF < iT1 || iF >= iT2)
         {
            bufferC[iF] = cmplx(0, 0);
         }
         else if (iF - iT1 < (wL - 1) / 2)
         {
            wdw = .42 - .5 * cos(2 * PI * (float) iS / ((float) (wL - 1))) +
                  .08 * cos(4 * PI * (float) iS / ((float) (wL - 1)));
	    bufferC[iF].r = dataS[iR][iF - iT1].r * wdw;
	    bufferC[iF].i = dataS[iR][iF - iT1].i * wdw;
            iS++;
         }
         else if (iF - iT1 >= info->nF - (wL - 1) / 2)
         {
            iS++;
            wdw = .42 - .5 * cos(2 * PI * (float) iS / ((float) (wL - 1))) +
                  .08 * cos(4 * PI * (float) iS / ((float) (wL - 1)));
	    bufferC[iF].r = dataS[iR][iF - iT1].r * wdw;
	    bufferC[iF].i = dataS[iR][iF - iT1].i * wdw;
         }
	 else
	 {
	    bufferC[iF] = dataS[iR][iF - iT1];
	 }
      }
      
      /* going to time domain */
      /* DD 
      fprintf(stderr, "going to time domain \n");*/

      pfacr(1, info->nSamples, bufferC, buffer);

      /* muting ? */
      if (MUTE)
      {
         for (iS = 0; iS <= NINT(t1Mute[iR] / dt); iS++)
         {
	    buffer[iS] = 0;
         }
      }

      /* and computing data misfit and likelihood function */
      iS = NINT(t1 / dt);
      for (iT1 = 0; iT1 < nDM; iT1++)
      {
	 bufferRCD[iT1 + iS] = 0;

	 for (offset = iT1, iT2 = 0; iT2 < nDM; iT2++)
	 {
	    bufferRCD[iT1 + iS] +=  
	                   (buffer[iT2 + iS] - dataObs[iR][iT2]) * CD[offset];
	    offset += MAX(SGN0(iT1 - iT2) * (nDM - 1 - iT2), 1);
	 }
	 oF += (buffer[iT1 + iS] - dataObs[iR][iT1]) * bufferRCD[iT1 + iS];

	 residue += (buffer[iT1 + iS] - dataObs[iR][iT1]) * 
                    (buffer[iT1 + iS] - dataObs[iR][iT1]);

	 /* DD 
	 fprintf(stdout, "%d %f %f %f %f %f %d %f %f\n", 
		 nTotalSamples, oF, dt, auxm1, 
		 info->tau, residue, iT1, buffer[iT1], 
		 dataObs[iR][iT1 - NINT(t1 / dt)]); */
      }

      /* windowing bufferRCD */
      iT1 = NINT(t1 / dt);
      iT2 = NINT(t2 / dt);
      wL = nDM * PERC_WINDOW / 2;
      wL = 2 * wL + 1;
      for (iS = 0, iF = 0; iF < info->nSamples; iF++)
      {
         if (iF < iT1 || iF >= iT2)
         {
            bufferRCD[iF] = 0;
         }
	 else if (iF - iT1 < (wL - 1) / 2)
         {
            wdw =
               .42 - .5 * cos(2 * PI * (float) iS / ((float) (wL - 1))) +
                  .08 * cos(4 * PI * (float) iS / ((float) (wL - 1)));
            bufferRCD[iF] *= wdw;
            iS++;
         }
         else if (iF - iT1 >= nDM - (wL - 1) / 2)
         {
            iS++;
            wdw =
               .42 - .5 * cos(2 * PI * (float) iS / ((float) (wL - 1))) +
                  .08 * cos(4 * PI * (float) iS / ((float) (wL - 1)));
            bufferRCD[iF] *= wdw;
         }
      }
      
      /* going back to Fourier domain */
      pfarc(-1, info->nSamples, bufferRCD, bufferC);          
      
      for (iF1 = 0, iF = NINT(info->f1 / info->dF); 
	   iF <= NINT(info->f2 / info->dF); iF++, iF1++)
      {
	 resCD[iR][iF1] = bufferC[iF];
      }
   }

   /* considering the .5 factor of the exponent of the Gaussian */
   /* and normalizing the likelihood by the number of samples */
   oF /= (2 * nTotalSamples);

   /* freeing some memory */
   /* allocating some memory */
   free2complex(dataS);
   free1float(buffer);
   free1float(bufferRCD);
   free1complex(bufferC);
   free2complex(freqPart);

   /* considering the regularizaton or model covariance term */
   if (PRIOR)
   {
      auxm1 = 1. / (float) (numberPar * limRange);     /* normalization */
      for (auxm2 = 0, iF = 0; iF < limRange; iF++)
      {
	 for (offset = iF, iF1 = 0; iF1 < limRange; iF1++)
	 {
	    if (vpFrechet)
	    {
	       auxm2 += (alpha[iF + lim[0]] - alphaMean[iF + lim[0]]) * 
		         CMvP[offset] * auxm1 * 
		        (alpha[iF1 + lim[0]] - alphaMean[iF1 + lim[0]]);
	    }
	    
	    if (vsFrechet)
	    {
	       auxm2 += (beta[iF + lim[0]] - betaMean[iF + lim[0]]) * 
	                 CMvS[offset] * auxm1 *
		        (beta[iF1 + lim[0]] - betaMean[iF1 + lim[0]]);
	    }
	    
	    if (rhoFrechet)
	    {
	       auxm2 += (rho[iF + lim[0]] - rhoMean[iF + lim[0]]) * 
		         CMrho[offset] * auxm1 *
		        (rho[iF1 + lim[0]] - rhoMean[iF1 + lim[0]]);
	    }
	    offset += MAX(SGN0(iF - iF1) * (limRange - 1 - iF1), 1);
	 }
      }
   }
   /* getting normalization factor */
   fp = fopen("report", "a");
   fprintf(fp,"-----------------------\n");

   if (modCount == 0) 
   {
      oFNorm = oF;
      fprintf(fp,">> Normalization constant for objective function: %f <<\n",
	      oFNorm);
   }
   
   /* normalizing residue */
   residue /= (nTotalSamples);

   if (!DATACOV && noiseVar == 0) noiseVar = residue / 10.;
   
   if (PRIOR)
   {
      fprintf(fp,
      "residue at iteration [%d] : Data residue variance %f , Noise variance %f , Likelihood %f , Prior %f\n", 
      modCount, residue, noiseVar, oF / oFNorm, auxm2 / oFNorm);
   }
   else
   {
      fprintf(fp,"residue at iteration [%d] : Data residue variance %f , Noise variance %f , Likelihood %f , No Prior\n", modCount, residue, noiseVar, oF / oFNorm);
   }

   /* checking if we reached noise variance with the data residue */
   if (residue / noiseVar <= 1)
   {
      /* DATA IS FIT, stop the procedure */
      fprintf(fp, "[][][][][][][][][][][][][][][][][][][][]\n");
      fprintf(fp, "DATA WAS FIT UP TO 1 VARIANCE!\n");
      fprintf(fp, "[][][][][][][][][][][][][][][][][][][][]\n");
      exit(0);
   }
   
   /* adding Likelihood and Prior */
   if (PRIOR) oF += auxm2 / 2;
   fprintf(fp,"TOTAL residue at iteration [%d] : %f\n", 
	   modCount, oF / oFNorm);

   fprintf(fp,"-----------------------\n");
   fclose(fp);


   /* returning objective function value */
   return(oF / oFNorm);
}
Пример #9
0
void 
population_fitness(struct pop_c* pop_conf, struct state** population)
{

	int ntask = MAXNCHILD;
	int info;
	int mytid;
	int child[MAXNCHILD];
	int i, j;
	mytid = pvm_mytid();

	if (mytid < 0) 
	{ 
		/* the perneially informative error message of my youth. */
		pvm_perror("wat"); 
		exit(-1);
	}

	/* spawn the child tasks */
	info = pvm_spawn("/home/rwblair/projects/parallel_ga/slave", (char**)0, 
	    PvmTaskDefault, (char*)0, ntask, child);
	/* only proceeds if all children properly spawned. It doesn't have to be
	 * this way but its easiest
	 */
	printf("result of pvm_spawn call: %d\n", info);
	printf("value of first index of child array: %d\n", child[0]);
	if (info != ntask) 
	{
		pvm_exit(); 
		exit(-1);
	}
	
	int tmp;	
	for (i = 0; i < ntask; i++)
	{
		tmp = ((pop_conf->pop_size)/ntask);
		if (i < (pop_conf->pop_size % ntask))
			tmp++;
		pvm_initsend(PvmDataDefault);
		pvm_pkint(&tmp, 1, 1);
		pvm_pkint(&(pop_conf->n), 1, 1);
		info = pvm_send(child[i], 0);
	}
	
	j = 0;
	for (i = 0; i < pop_conf->pop_size; i++) 
	{
		pvm_initsend(PvmDataDefault);
		pvm_pkint(&i, 1, 1);
		pvm_pkint(population[i]->configuration, pop_conf->n, 1);

		if ((i == pop_conf->pop_size -1) || 
		    (((pop_conf->pop_size / MAXNCHILD)) * (j + 1)) - 1 == i)
		{
			info = pvm_send(child[j++], 0);
		}
	}


	int recv_ret;
	int recv_index;	
	int recv_fit;
	/* error handling, if a given thing f***s up how can it be unfucked? */
	for (i = 0; i < pop_conf->pop_size; i++)
	{
		recv_ret = pvm_recv(-1, -1);
		pvm_upkint(&recv_index, 1, 1);
		pvm_upkint(&recv_fit, 1, 1);	
		printf("recieved fitness: %d\n", recv_fit);
		population[recv_index]->fitness = recv_fit;
		/*
		if ((recv_index < 0) || (recv_index >= pop_conf->pop_size))
			printf("BAD RECV_INDEX\n");
		*/
	}

	pvm_exit();
	return;

}
Пример #10
0
void
main (int argc, char **argv)
{
   /* declaration of variables */
   FILE *fp;                     /* file pointer */
   char *auxChar;                /* auxiliar character */
   char *modelFile = " ";        /* elastic model file */
                                 /* THICK - RHO - VP - QP - VS - QS */
   int i, k, iProc, iR;          /* counters */
   int initF, lastF;             /* initial and final frequencies */
   int apl_pid;                  /* PVM process id control */
   int nSamplesOrig;             /* time series length */
   int die;                      /* flag used to kill processes */
   int pid;                      /* process id */
   int nProc;                    /* number of processes */
   int processControl;           /* monitoring PVM start */
   int *processes;               /* array with process ids */
   int FReceived;                /* number of frequencies processed */
   int nFreqProc;                /* number of frequencies per process */
   int nFreqPart;                /* number of frequency partitions */
   int **statusFreq;             /* monitors processed frequencies */
   int FInfo[2];                 /* frequency delimiters */
   int **procInfo;               /* frequency limits for each processor */ 
   float wallcpu;                /* wall clock time */
   float dt;                     /* time sampling interval */
   float f;                      /* current frequency */
   float fR;                     /* reference frequency */
   float tMax;                   /* maximum recording time */
   float *thick, *alpha, *beta,
   *rho, *qP, *qS;               /* elastic constants and thickness */
   complex **freqPart;           /* frequency arrays sent by the slaves */
   complex **uRF, **uZF;         /* final frequency components */
   INFO info[1];                 /* basic information for slaves */
   
   /* Logging information */
   CleanLog();

   /* getting input */
   initargs(argc, argv);
   requestdoc(0);
   
   if (!getparstring("model", &modelFile)) modelFile = "model";
   if (!getparstring("recfile", &auxChar)) auxChar = " ";
   sprintf(info->recFile, "%s", auxChar);
   if (!getparint("directwave", &info->directWave)) info->directWave = 1;
   if (!getparfloat("r1", &info->r1)) info->r1 = 0;
   if (!getparint("nr", &info->nR)) info->nR = 148;
   if (!getparfloat("dr", &info->dR)) info->dR = .025;
   if (!getparfloat("zs", &info->zs)) info->zs = 0.001;
   if (info->zs <= 0) info->zs = 0.001;
   if (!getparfloat("u1", &info->u1)) info->u1 = 0.0002;
   if (!getparfloat("u2", &info->u2)) info->u2 = 1.;
   if (!getparint("nu", &info->nU)) info->nU = 1000;
   if (!getparfloat("f1", &info->f1)) info->f1 = 2;
   if (!getparfloat("f2", &info->f2)) info->f2 = 50;
   if (!getparfloat("dt", &dt)) dt = 0.004;
   if (!getparfloat("tmax", &tMax)) tMax = 8;
   if (!getparfloat("F1", &info->F1)) info->F1 = 0;
   if (!getparfloat("F2", &info->F2)) info->F2 = 0;
   if (!getparfloat("F3", &info->F3)) info->F3 = 1;
   if (!getparint("hanning", &info->hanningFlag)) info->hanningFlag = 0;
   if (!getparfloat("wu", &info->percU)) info->percU = 5; info->percU /= 100;
   if (!getparfloat("ww", &info->percW)) info->percW = 5; info->percW /= 100;
   if (!getparfloat("fr", &fR)) fR = 1; info->wR = 2 * PI * fR;
   if (!getparfloat("tau", &info->tau)) info->tau = 50;
   if (!getparint("nproc", &nProc)) nProc = 1;
   if (!getparint("nfreqproc", &nFreqProc) || nProc == 1) nFreqProc = 0;
   if (!getparint("verbose", &info->verbose)) info->verbose = 0;

   /* how many layers */
   fp = fopen(modelFile,"r");
   if (fp == NULL)
      err("No model file!\n");

   info->nL = 0;
   while (fscanf(fp, "%f %f %f %f %f %f\n", 
		 &f, &f, &f, &f, &f, &f) != EOF)
      info->nL++;
   info->nL--;
   fclose(fp);

   if (info->verbose)
      fprintf(stderr,"Number of layers in model %s : %d\n", 
	      modelFile, info->nL + 1); 
   
   /* if specific geometry, count number of receivers */
   fp = fopen(info->recFile, "r");
   if (fp != NULL)
   {
      info->nR = 0;
      while (fscanf(fp, "%f\n", &f) != EOF)
	 info->nR++;
   fclose(fp);
   }

   /* memory allocation */
   alpha = alloc1float(info->nL + 1);
   beta = alloc1float(info->nL + 1);
   rho = alloc1float(info->nL + 1);
   qP = alloc1float(info->nL + 1);
   qS = alloc1float(info->nL + 1);
   thick = alloc1float(info->nL + 1);
   processes = alloc1int(nProc);
   procInfo = alloc2int(2, nProc);

   /* reading the file */
   fp = fopen(modelFile,"r");
   if (info->verbose)
      fprintf(stderr,"Thickness     rho     vP     qP    vS     qS\n");
   for (i = 0; i < info->nL + 1; i++)
   {
      fscanf(fp, "%f %f %f %f %f %f\n", &thick[i], &rho[i], &alpha[i], 
	     &qP[i], &beta[i], &qS[i]);
      if (info->verbose)
	 fprintf(stderr,"   %7.4f      %4.3f   %3.2f  %5.1f  %3.2f  %5.1f\n",
		 thick[i], rho[i], alpha[i], qP[i], beta[i], qS[i]);
   }
   fclose(fp);

   /* computing frequency interval */
   info->nSamples = NINT(tMax / dt) + 1;
   nSamplesOrig = info->nSamples;
   info->nSamples = npfar(info->nSamples);

   /* slowness increment */
   info->dU = (info->u2 - info->u1) / (float) info->nU;

   /* computing more frequency related quatities */
   tMax = dt * (info->nSamples - 1);
   info->dF = 1. / (tMax);   
   f = info->dF;
   while (f < info->f1) f += info->dF;
   info->f1 = f;
   while (f < info->f2) f += info->dF;
   info->f2 = f; 
   initF = NINT(info->f1 / info->dF);
   lastF = NINT(info->f2 / info->dF);

   info->nF = NINT(info->f2 / info->dF) - NINT(info->f1 / info->dF) + 1;

   if (info->nF%2 == 0)
   {
      info->f2 += info->dF;
      info->nF++;
   }
   initF = NINT(info->f1 / info->dF);
   lastF = NINT(info->f2 / info->dF);  

   /* attenuation of wrap-around */
   info->tau = log(info->tau) / tMax;
   if (info->tau > TAUMAX)
      info->tau = TAUMAX;
      
   if (info->verbose)
      fprintf(stderr, "Discrete frequency range to model: [%d, %d]\n", 
	      initF, lastF);
   
   if (nFreqProc == 0)
   {
      nFreqProc = NINT((float) info->nF / (float) nProc + .5);
      if (nFreqProc > info->nF) nFreqProc = info->nF;
   }
   else
      while (nFreqProc > info->nF) nFreqProc /= 2;
   nFreqPart = NINT((float) info->nF / (float) nFreqProc + .5);

   /* memory allocation for frequency arrays */
   uRF = alloc2complex(info->nSamples / 2 + 1, info->nR);
   uZF = alloc2complex(info->nSamples / 2 + 1, info->nR);
   freqPart = alloc2complex(nFreqProc, info->nR);
   statusFreq = alloc2int(3, nFreqPart);

   /* defining frequency partitions */
   for (k = initF, i = 0; i < nFreqPart; i++, k += nFreqProc)
   {
      statusFreq[i][0] = k;
      statusFreq[i][1] = MIN(k + nFreqProc - 1, lastF);
      statusFreq[i][2] = 0;       
   }

   if (info->verbose)
      fprintf(stderr, "Starting communication with PVM\n");
   
   /* starting communication with PVM */
   if ((apl_pid = pvm_mytid()) < 0) 
   {
      pvm_perror("Error enrolling master process");
      exit(-1);
   } 
   processControl = CreateSlaves(processes, PROCESS, nProc);
   if (processControl != nProc)
   {
      fprintf(stderr,"Problem starting PVM daemons\n");
      exit(-1);
   }

   info->nFreqProc = nFreqProc;
   /* Broadcasting all processes common information */
   BroadINFO(info, 1, processes, nProc, GENERAL_INFORMATION);
   
   if (info->verbose)
      fprintf(stderr, "Broadcasting model information to all slaves\n");

   /* sending all profiles */
   BroadFloat(thick, info->nL + 1, processes, nProc, THICKNESS);
   BroadFloat(rho, info->nL + 1, processes, nProc, DENSITY);
   BroadFloat(alpha, info->nL + 1, processes, nProc, ALPHA);
   BroadFloat(qP, info->nL + 1, processes, nProc, QALPHA);
   BroadFloat(beta, info->nL + 1, processes, nProc, BETA);
   BroadFloat(qS, info->nL + 1, processes, nProc, QBETA);

   /* freeing memory */
   free1float(thick);
   free1float(rho);
   free1float(alpha);
   free1float(qP);
   free1float(beta);
   free1float(qS);

   /* sending frequency partitions for each process */
   for (iProc = 0; iProc < nProc; iProc++)
   {
      FInfo[0] = statusFreq[iProc][0];
      FInfo[1] = statusFreq[iProc][1];

      if (info->verbose)
	 fprintf(stderr, 
	 "Master sending frequencies [%d, %d] out of %d to slave %d [id:%d]\n"
	  ,FInfo[0], FInfo[1], info->nF, iProc, processes[iProc]);

      procInfo[iProc][0] = FInfo[0]; procInfo[iProc][1] = FInfo[1];
      SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
      statusFreq[iProc][2] = 1;
   }

   /* waiting modelled frequencies */
   /* master process will send more frequencies if there's more work to do */
   /* measuring elapsed time */
   wallcpu = walltime();  
   
   /* reseting frequency counter */
   FReceived = 0;
   
   while (FOREVER)
   {
      pid = RecvCplx(freqPart[0], info->nR * nFreqProc, -1, 
		     FREQUENCY_PARTITION_VERTICAL);

      /* finding the frequency limits of this process */
      iProc = 0;
      while (pid != processes[iProc])
	 iProc++;

      /* copying into proper place of the total frequency array */
      for (iR = 0; iR < info->nR; iR++)
      {
	 for (k = 0, i = procInfo[iProc][0]; i <= procInfo[iProc][1]; i++, k++)
	 {
	    uZF[iR][i] = freqPart[iR][k];
	 }
      }

      pid = RecvCplx(freqPart[0], info->nR * nFreqProc, -1, 
		     FREQUENCY_PARTITION_RADIAL);
      
      /* finding the frequency limits of this process */
      iProc = 0;
      while (pid != processes[iProc])
	 iProc++;
   
      /* copying into proper place of the total frequency array */
      for (iR = 0; iR < info->nR; iR++)
      { 
	 for (k = 0, i = procInfo[iProc][0]; i <= procInfo[iProc][1]; i++, k++)
	 {
	    uRF[iR][i] = freqPart[iR][k];
	 }
      }

      /* summing frequencies that are done */
      FReceived += procInfo[iProc][1] - procInfo[iProc][0] + 1;

      if (info->verbose)
	 fprintf(stderr, "Master received %d frequencies, remaining %d\n", 
	      FReceived, info->nF - FReceived);

/*       if (FReceived >= info->nF) break; */

      /* defining new frequency limits */
      i = 0;
      while (i < nFreqPart && statusFreq[i][2])
	 i++;
      
      if (i < nFreqPart)
      {
	 /* there is still more work to be done */
	 /* tell this process to not die */
	 die = 0;
	 SendInt(&die, 1, processes[iProc], DIE);

	 FInfo[0] = statusFreq[i][0];
	 FInfo[1] = statusFreq[i][1];

	 if (info->verbose)
	    fprintf(stderr, 
		    "Master sending frequencies [%d, %d] to slave %d\n", 
		    FInfo[0], FInfo[1], processes[iProc]);
	 
	 procInfo[iProc][0] = FInfo[0]; procInfo[iProc][1] = FInfo[1];
	 SendInt(FInfo, 2, processes[iProc], FREQUENCY_LIMITS);
	 statusFreq[i][2] = 1;
      }
      else
      {
	 /* tell this process to die since there is no more work to do */
	 if (info->verbose)
	    fprintf(stderr, "Master ''killing'' slave %d\n", processes[iProc]);
	 die = 1;
	 SendInt(&die, 1, processes[iProc], DIE);
      }
      
      /* a check to get out the loop */
      if (FReceived >= info->nF) break; 
   }

   if (info->verbose)
      fprintf(stderr, "Master ''killing'' remaining slaves\n");

   /* getting elapsed time */
   wallcpu = walltime() - wallcpu;
   fprintf(stderr, "Wall clock time = %f seconds\n", wallcpu);  
   
   /* going to time domain */
   memset( (void *) &trZ, (int) '\0', sizeof(trZ));     
   memset( (void *) &trR, (int) '\0', sizeof(trR));     
   trZ.dt = dt * 1000000;
   trZ.ns = nSamplesOrig;
   trR.dt = dt * 1000000;
   trR.ns = nSamplesOrig;
   
   /* z component */
   for (iR = 0; iR < info->nR; iR++)
   {
      trZ.tracl = iR + 1;
      /* inverse FFT */
      pfacr(1, info->nSamples, uZF[iR], trZ.data); 
      for (i = 0; i < info->nSamples; i++)
      {
	 /* compensating for the complex frequency */
	 trZ.data[i] *= exp(info->tau * i * dt);
      }
      puttr(&trZ);
   }

   /* r component */
   for (iR = 0; iR < info->nR; iR++)
   {
      trR.tracl = iR + 1;
      /* inverse FFT */
      pfacr(1, info->nSamples, uRF[iR], trR.data); 
      for (i = 0; i < info->nSamples; i++)
      {
	 /* compensating for the complex frequency */
	 trR.data[i] *= exp(info->tau * i * dt);
      }
      puttr(&trR);
   }
}   
Пример #11
0
void BBSClient::perror(const char* s) {
	pvm_perror((char*)s);
}