Exemplo n.º 1
0
void Decomp_Sync_Negotiate_RemoteSearch( Decomp_Sync_Negotiate* self, Decomp_Sync* sync, 
					 unsigned** nRemFound, unsigned*** remFound )
{
	unsigned*	nFound;
	unsigned**	found;
	RangeSet*	rSet;
	unsigned	nBytes;
	Stg_Byte*	bytes;
	unsigned*	remNBytes;
	unsigned**	remBytes;
	RangeSet**	remSets;
	unsigned	p_i;

	assert( self );
	assert( sync );
	assert( nRemFound );
	assert( remFound );

	if( sync->commTopo->nInc ) {
		/* Build a range set of our remotes and pickle. */
		rSet = RangeSet_New();
		RangeSet_SetIndices( rSet, sync->nRemotes, sync->remotes );
		RangeSet_Pickle( rSet, &nBytes, &bytes );

		/* Broadcast our remotes to neighbours. */
		CommTopology_Allgather( sync->commTopo, 
					nBytes, bytes, 
					&remNBytes, (void***)&remBytes, 
					sizeof(Stg_Byte) );

		/* Free bytes. */
		FreeArray( bytes );

		/* Unpickle range sets. */
		remSets = Memory_Alloc_Array_Unnamed( RangeSet*, sync->commTopo->nInc );
		for( p_i = 0; p_i < sync->commTopo->nInc; p_i++ ) {
			remSets[p_i] = RangeSet_New();
			RangeSet_Unpickle( remSets[p_i], remNBytes[p_i], (void*)remBytes[p_i] );
		}

		/* Free remote bytes. */
		FreeArray( remNBytes );
		FreeArray( remBytes );

		/* Replace our remote range set with a local one, then intersect remote sets. */
		RangeSet_SetIndices( rSet, sync->decomp->nLocals, sync->decomp->locals );
		nFound = Memory_Alloc_Array_Unnamed( unsigned, sync->commTopo->nInc );
		found = Memory_Alloc_Array_Unnamed( unsigned*, sync->commTopo->nInc );
		for( p_i = 0; p_i < sync->commTopo->nInc; p_i++ ) {
			RangeSet_Intersection( remSets[p_i], rSet );
			RangeSet_GetIndices( remSets[p_i], nFound + p_i, found + p_i );
			FreeObject( remSets[p_i] );
		}

		/* Free remote set array. */
		FreeArray( remSets );

		/* Send back all the ones we found and receive from all others all our requireds they found. */
		CommTopology_Alltoall( sync->commTopo, 
				       nFound, (void**)found, 
				       nRemFound, (void***)remFound, 
				       sizeof(unsigned) );

		/* Release some memory. */
		FreeArray( nFound );
		FreeArray2D( sync->commTopo->nInc, found );
	}
Exemplo n.º 2
0
/*>BOOL ReadMDM(char *mdmfile)
   ---------------------------
   Input:   char  *mdmfile    Mutation data matrix filename
   Returns: BOOL              Success?
   
   Read mutation data matrix into static global arrays. The matrix may
   have comments at the start introduced with a ! in the first column.
   The matrix must be complete (i.e. a triangular matrix will not
   work). A line describing the residue types must appear, and may
   be placed before or after the matrix itself

   07.10.92 Original
   18.03.94 getc() -> fgetc()
   24.11.94 Automatically looks in DATAENV if not found in current 
            directory
   28.02.95 Modified to read any size MDM and allow comments
            Also allows the list of aa types before or after the actual
            matrix
   26.07.95 Removed unused variables
   06.02.03 Fixed for new version of GetWord()
   07.04.09 Completely re-written to allow it to read BLAST style matrix
            files as well as the ones used previously
            Allow comments introduced with # as well as !
            Uses MAXWORD rather than hardcoded 16
*/
BOOL ReadMDM(char *mdmfile)
{
   FILE *mdm = NULL;
   int  i, j, k, row, tmpStoreSize;
   char buffer[MAXBUFF],
        word[MAXWORD],
        *p,
        **tmpStore;
   BOOL noenv;

   if((mdm=OpenFile(mdmfile, DATAENV, "r", &noenv))==NULL)
   {
      return(FALSE);
   }

   /* First read the file to determine the dimensions                   */
   while(fgets(buffer,MAXBUFF,mdm))
   {
      TERMINATE(buffer);
      KILLLEADSPACES(p,buffer);

      /* First line which is non-blank and non-comment                  */
      if(strlen(p) && p[0] != '!' && p[0] != '#')
      {
         sMDMSize = 0;
         for(p = buffer; p!=NULL;)
         {
            p = GetWord(p, word, MAXWORD);
            /* Increment counter if this is numeric                     */
            if(isdigit(word[0]) || 
               ((word[0] == '-')&&(isdigit(word[1]))))
               sMDMSize++;
         }
         if(sMDMSize)
            break;
      }
   }

   /* Allocate memory for the MDM and the AA List                       */
   if((sMDMScore = (int **)Array2D(sizeof(int),sMDMSize,sMDMSize))==NULL)
      return(FALSE);
   if((sMDM_AAList = (char *)malloc((sMDMSize+1)*sizeof(char)))==NULL)
   {
      FreeArray2D((char **)sMDMScore, sMDMSize, sMDMSize);
      return(FALSE);
   }

   /* Allocate temporary storage for a row from the matrix              */
   tmpStoreSize = 2*sMDMSize;
   if((tmpStore = (char **)Array2D(sizeof(char), tmpStoreSize, MAXWORD))
      ==NULL)
   {
      free(sMDM_AAList);
      FreeArray2D((char **)sMDMScore, sMDMSize, sMDMSize);
      return(FALSE);
   }

   /* Fill the matrix with zeros                                        */
   for(i=0; i<sMDMSize; i++)
   {
      for(j=0; j<sMDMSize; j++)
      {
         sMDMScore[i][j] = 0;
      }
   }

   /* Rewind the file and read the actual data                          */
   rewind(mdm);
   row = 0;
   while(fgets(buffer,MAXBUFF,mdm))
   {
      int Numeric;
      
      TERMINATE(buffer);
      KILLLEADSPACES(p,buffer);

      /* Check line is non-blank and non-comment                        */
      if(strlen(p) && p[0] != '!' && p[0] != '#')
      {
         Numeric = 0;
         for(p = buffer, i = 0; p!=NULL && i<tmpStoreSize; i++)
         {
            p = GetWord(p, tmpStore[i], MAXWORD);
            /* Incremement Numeric counter if it's a numeric field      */
            if(isdigit(tmpStore[i][0]) || 
               ((tmpStore[i][0] == '-')&&(isdigit(tmpStore[i][1]))))
            {
               Numeric++;
            }
         }

         /* No numeric fields so it is the amino acid names             */
         if(Numeric == 0)
         {
            for(j = 0; j<i && j<sMDMSize; j++)
            {
               sMDM_AAList[j] = tmpStore[j][0];
            }
         }
         else
         {
            /* There were numeric fields, so copy them into the matrix,
               skipping any non-numeric fields
               j counts the input fields
               k counts the fields in sMDMScore
               row counts the row in sMDMScore
            */
            for(j=0, k=0; j<i && k<sMDMSize; j++)
            {
               if(isdigit(tmpStore[j][0]) || 
                  ((tmpStore[j][0] == '-')&&(isdigit(tmpStore[j][1]))))
               {
                  sscanf(tmpStore[j],"%d",&(sMDMScore[row][k]));
                  k++;
               }
            }
            
            row++;
         }
      }
   }
   fclose(mdm);
   FreeArray2D((char **)tmpStore, tmpStoreSize, MAXWORD);
   
   return(TRUE);
}
Exemplo n.º 3
0
void Decomp_Sync_Negotiate_Select( void* negotiate, Decomp_Sync* sync ) {
	Decomp_Sync_Negotiate*	self = (Decomp_Sync_Negotiate*)negotiate;
	CommTopology*		commTopo;
	unsigned		rank;
	unsigned		nInc;
	unsigned*		inc;
	unsigned*		nFound;
	unsigned**		found;
	unsigned*		nUniques;
	unsigned**		uniques;
	unsigned		mostUniques;
	unsigned**		srcs;
	unsigned		p_i;

	assert( self );
	assert( sync );
	assert( sync->commTopo );

	/* Shortcuts. */
	commTopo = sync->commTopo;
	MPI_Comm_rank( commTopo->comm, (int*)&rank );

	/* Locate remote indices. */
	Decomp_Sync_Negotiate_RemoteSearch( self, sync, &nFound, &found );

	/* Get processor incidence. */
	CommTopology_GetIncidence( commTopo, rank, &nInc, &inc );

	if( nInc ) {
		/* Set up the source arrays. */
		sync->nSrcs = Memory_Alloc_Array( unsigned, nInc, "Decomp_Sync::nSrcs" );
		srcs = Memory_Alloc_Array_Unnamed( unsigned*, nInc );
		memset( sync->nSrcs, 0, nInc * sizeof(unsigned) );
		memset( srcs, 0, nInc * sizeof(unsigned*) );

		/* Select source procs from which to retrieve required nodes such that communications are minimized. */
		nUniques = Memory_Alloc_Array_Unnamed( unsigned, nInc );
		uniques = Memory_Alloc_2DComplex_Unnamed( unsigned, nInc, nFound );
		memset( nUniques, 0, nInc * sizeof(unsigned) );

		do {
			unsigned	mostProc = 0;

			/* Loop over procs and find unique nodes per proc. ie. the most nodes not already covered by
			   an existing source. */
			mostUniques = 0;
			for( p_i = 0; p_i < nInc; p_i++ ) {
				unsigned	fnd_i;

				/* Clear the number of uniques. */
				nUniques[p_i] = 0;

				/* If there are no founds for this proc or we've already sourced it, then skip. */
				if( nFound[p_i] == 0 || sync->nSrcs[p_i] > 0 )
					continue;

				/* Hunt down unique global indices. */
				for( fnd_i = 0; fnd_i < nFound[p_i]; fnd_i++ ) {
					unsigned	p_j;

					for( p_j = 0; p_j < nInc; p_j++ ) {
						unsigned	src_i;

						for( src_i = 0; src_i < sync->nSrcs[p_i]; src_i++ ) {
							if( srcs[p_i][src_i] == found[p_i][fnd_i] )
								break;
						}
						if( src_i < sync->nSrcs[p_i] )
							break;
					}
					if( p_j == nInc )
						uniques[p_i][nUniques[p_i]++] = found[p_i][fnd_i];
				}

				/* Determine which proc has the most uniques and store. */
				if( nUniques[p_i] > mostUniques ) {
					mostUniques = nUniques[p_i];
					mostProc = p_i;
				}
			}

			/* Store result. */
			if( mostUniques ) {
				sync->nSrcs[mostProc] = mostUniques;
				if( mostUniques ) {
					srcs[mostProc] = Memory_Alloc_Array_Unnamed( unsigned, mostUniques );
					memcpy( srcs[mostProc], uniques[mostProc], mostUniques * sizeof(unsigned) );
				}
			}
		}
		while( mostUniques );

		/* Proper allocation. */
		sync->srcs = Memory_Alloc_2DComplex( unsigned, nInc, sync->nSrcs, "Decomp_Sync::srcs" );
		for( p_i = 0; p_i < nInc; p_i++ ) {
			if( sync->nSrcs[p_i] )
				memcpy( sync->srcs[p_i], srcs[p_i], sync->nSrcs[p_i] * sizeof(unsigned) );
		}

		/* Get rid of all the resources so far. */
		FreeArray2D( nInc, srcs );
		FreeArray( nUniques );
		FreeArray( uniques );

		/* Communicate back to all procs our selection.  Note that even though most procs will not need to send
		   anything here, we still need to let them know that. */
		CommTopology_Alltoall( commTopo, 
				       sync->nSrcs, (void**)sync->srcs, 
				       &sync->nSnks, (void***)&sync->snks, 
				       sizeof(unsigned) );

		/* Calculate net values for source and sink. */
		for( p_i = 0; p_i < nInc; p_i++ ) {
			sync->netSrcs += sync->nSrcs[p_i];
			sync->netSnks += sync->nSnks[p_i];
		}
	}

	/* Return incidence. */
	CommTopology_ReturnIncidence( commTopo, rank, &nInc, &inc );
}
Exemplo n.º 4
0
/*>int affinealign(char *seq1, int length1, char *seq2, int length2, 
                   BOOL verbose, BOOL identity, int penalty, int penext, 
                   char *align1, char *align2, int *align_len)
   ---------------------------------------------------------------------
   Input:   char  *seq1         First sequence
            int   length1       First sequence length
            char  *seq2         Second sequence
            int   length2       Second sequence length
            BOOL  verbose       Display N&W matrix
            BOOL  identity      Use identity matrix
            int   penalty       Gap insertion penalty value
            int   penext        Extension penalty
   Output:  char  *align1       Sequence 1 aligned
            char  *align2       Sequence 2 aligned
            int   *align_len    Alignment length
   Returns: int                 Alignment score (0 on error)
            
   Perform simple N&W alignment of seq1 and seq2. No window is used, so
   will be slow for long sequences.

   Note that you must allocate sufficient memory for the aligned 
   sequences.
   The easy way to do this is to ensure that align1 and align2 are
   of length (length1+length2).

   07.10.92 Adapted from original written while at NIMR
   08.10.92 Split into separate routines
   09.10.92 Changed best structure to simple integers, moved 
            SearchForBest() into TraceBack()
   21.08.95 Was only filling in the bottom right cell at initialisation
            rather than all the right hand column and bottom row
   11.07.96 Changed calls to calcscore() to CalcMDMScore()
   06.03.00 Changed name to affinealign() (the routine align() is
            provided as a backwards compatible wrapper). Added penext 
            parameter. Now supports affine gap penalties with separate
            opening and extension penalties. The code now maintains
            the path as it goes.
   27.02.07 Exactly as affinealign() but upcases characters before
            comparison

**************************************************************************
******    NOTE AND CHANGES SHOULD BE PROPAGATED TO affinealign()    ******
**************************************************************************
*/
int affinealignuc(char *seq1, 
                  int  length1, 
                  char *seq2, 
                  int  length2, 
                  BOOL verbose, 
                  BOOL identity, 
                  int  penalty, 
                  int  penext,
                  char *align1, 
                  char *align2,
                  int  *align_len)
{
   XY    **dirn   = NULL;
   int   **matrix = NULL,
         maxdim,
         i,    j,    k,    l,
         i1,   j1,
         dia,  right, down,
         rcell, dcell, maxoff,
         match = 1,
         thisscore,
         gapext,
         score;
   
   maxdim = MAX(length1, length2);
   
   /* Initialise the score matrix                                       */
   if((matrix = (int **)Array2D(sizeof(int), maxdim, maxdim))==NULL)
      return(0);
   if((dirn   = (XY **)Array2D(sizeof(XY), maxdim, maxdim))==NULL)
      return(0);
      
   for(i=0;i<maxdim;i++)
   {
      for(j=0;j<maxdim;j++)
      {
         matrix[i][j] = 0;
         dirn[i][j].x = -1;
         dirn[i][j].y = -1;
      }
   }
    
   /* Fill in scores up the right hand side of the matrix               */
   for(j=0; j<length2; j++)
   {
      if(identity)
      {
         if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
      }
      else
      {
         matrix[length1-1][j] = CalcMDMScoreUC(seq1[length1-1], seq2[j]);
      }
   }

   /* Fill in scores along the bottom row of the matrix                 */
   for(i=0; i<length1; i++)
   {
      if(identity)
      {
         if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
      }
      else
      {
         matrix[i][length2-1] = CalcMDMScoreUC(seq1[i], seq2[length2-1]);
      }
   }

   i = length1 - 1;
   j = length2 - 1;
   
   /* Move back along the diagonal                                      */
   while(i > 0 && j > 0)
   {
      i--;
      j--;

      /* Fill in the scores along this row                              */
      for(i1 = i; i1 > -1; i1--)
      {
         dia   = matrix[i1+1][j+1];

         /* Find highest score to right of diagonal                     */
         rcell = i1+2;
         if(i1+2 >= length1)  right = 0;
         else                 right = matrix[i1+2][j+1] - penalty;
         
         gapext = 1;
         for(k = i1+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j+1] - (penalty + gapext*penext);
            
            if(thisscore > right) 
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j+2;
         if(j+2 >= length2)  down = 0;
         else                down   = matrix[i1+1][j+2] - penalty;
         
         gapext = 1;
         for(l = j+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i1+1][l] - (penalty + gapext*penext);

            if(thisscore > down) 
            {
               down = thisscore;
               dcell = l;
            }
         }
         
         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i1][j] = dia;
            dirn[i1][j].x = i1+1;
            dirn[i1][j].y = j+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i1][j] = right;
               dirn[i1][j].x = rcell;
               dirn[i1][j].y = j+1;
            }
            else
            {
               matrix[i1][j] = down;
               dirn[i1][j].x = i1+1;
               dirn[i1][j].y = dcell;
            }
         }
       
         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
         }
         else
         {
            matrix[i1][j] += CalcMDMScoreUC(seq1[i1],seq2[j]);
         }
      }

      /* Fill in the scores in this column                              */
      for(j1 = j; j1 > -1; j1--)
      {
         dia   = matrix[i+1][j1+1];
         
         /* Find highest score to right of diagonal                     */
         rcell = i+2;
         if(i+2 >= length1)   right = 0;
         else                 right = matrix[i+2][j1+1] - penalty;

         gapext = 1;
         for(k = i+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j1+1] - (penalty + gapext*penext);
            
            if(thisscore > right) 
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j1+2;
         if(j1+2 >= length2)  down = 0;
         else                 down = matrix[i+1][j1+2] - penalty;

         gapext = 1;
         for(l = j1+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i+1][l] - (penalty + gapext*penext);
            
            if(thisscore > down) 
            {
               down = thisscore;
               dcell = l;
            }
         }

         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i][j1] = dia;
            dirn[i][j1].x = i+1;
            dirn[i][j1].y = j1+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i][j1] = right;
               dirn[i][j1].x = rcell;
               dirn[i][j1].y = j1+1;
            }
            else
            {
               matrix[i][j1] = down;
               dirn[i][j1].x = i+1;
               dirn[i][j1].y = dcell;
            }
         }
       
         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
         }
         else
         {
            matrix[i][j1] += CalcMDMScoreUC(seq1[i],seq2[j1]);
         }
      }
   } 
   
   score = TraceBack(matrix, dirn, length1, length2,
                     seq1, seq2, align1, align2, align_len);

   if(verbose)
   {
      printf("Matrix:\n-------\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("%3d ",matrix[i][j]);
         }
         printf("\n");
      }

      printf("Path:\n-----\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
         }
         printf("\n");
      }
   }
    
   FreeArray2D((char **)matrix, maxdim, maxdim);
   FreeArray2D((char **)dirn,   maxdim, maxdim);
    
   return(score);
}