Beispiel #1
0
/*************************************************************************
* This function writes a graphs into a file 
**************************************************************************/
void WriteMocGraph(GraphType *graph)
{
  idxtype i, j, nvtxs, ncon;
  idxtype *xadj, *adjncy;
  float *nvwgt;
  char filename[256];
  FILE *fpout;

  nvtxs = graph->nvtxs;
  ncon = graph->ncon;
  xadj = graph->xadj;
  adjncy = graph->adjncy;
  nvwgt = graph->nvwgt;

  msprintf(filename, "moc.graph.%D.%D", nvtxs, ncon);

  fpout = gk_fopen(filename, "w", __func__);

  mfprintf(fpout, "%D %D 10 1 %D", nvtxs, xadj[nvtxs]/2, ncon);

  for (i=0; i<nvtxs; i++) {
    mfprintf(fpout, "\n");
    for (j=0; j<ncon; j++)
      fprintf(fpout, "%" PRIIDX " ", (int)((float)10e6*nvwgt[i*ncon+j]));

    for (j=xadj[i]; j<xadj[i+1]; j++)
      fprintf(fpout, " %" PRIIDX, adjncy[j]+1);
  }

  gk_fclose(fpout);
}
char *parseParam(MFILE *mfout, Content *insat, const char *params, int printpname)
{
	char *format=(char*)params, *name, *comment=NULL, *cend;
	if(secakt==NULL) pexit("Param: tried to insert, but no section found\n", params);
	
	name=strchr(format, ' ');
	if(name==NULL) pexit("Param-Format not found - exiting\n", params);
	*name=0; name++;
	if(*name==0) pexit("Param-Name not found - exiting\n", name);

	cend=strchr(name, ' ');
	if(cend!=NULL){
		*cend=0; cend++;
		while(*cend==' ') cend++;

		if(*cend=='\''){
			comment=cend;
			comment++;
			cend=strchr(comment, '\'');
			if(cend==NULL) pexit("Param-Comment not ended with ' - exiting\n", params);
			*cend=0; cend++;
			while(*cend!=0 && *cend==' ') cend++;
		}
	}

	listInsertContent(insat, format, name, comment);
	if(printpname)
		mfprintf(mfout, "%s=%%%s", name, format);
	else
		mfprintf(mfout, "%%%s", format);

	if(cend==NULL || *cend==0) return(NULL);
	return(cend);
}
Beispiel #3
0
void check_active_enemies()
{
   static int timer;

   if(_state.total_enemies == 0){
      if(_state.player_type == PT_OGGER || _state.player_type == PT_DOGFIGHTER){
	 timer ++;
	 if(timer == 100){
	    mfprintf(stderr, "nobody to fight\n");
	    exitRobot(0);
	 }
      }
      if(_state.timer_delay_ms < 10.0){
	 mprintf("No enemies: setting 1 update per second\n");
	 sendUpdatePacket(999999);
	 _state.timer_delay_ms = 10.0;
      }
   }
   else {
      timer = 0;
   
      if(_state.timer_delay_ms > updates){
	 _state.timer_delay_ms = updates;
	 mprintf("Enemies: setting %d updates per second\n",
		(int)(1000000./(updates * 100000.)));
	 sendUpdatePacket((int)(updates * 100000.));
      }
   }
}
MFILE *fileRead(char *fname)
{
	MFILE *mfin=mfopen(), *mfout=mfopen(), *mfbuf=mfopen();
	FILE *f=fopen(fname, "r");
	const char *flastok, *fhit, *fend;
	if(f==NULL) pexit("Cannot read input file ", fname);
	
	mfFileToMFile(f, mfin);
	fclose(f);
	
	flastok=fhit=fend=mfGetData(mfin);
	while((fhit=strstr(flastok, "<!--#"))!=NULL){
		searchInput(flastok, fhit);
	
		fend=strstr(fhit, "-->");
		if(fend==NULL) pexit("Parse error - '-->' expected - exiting\n", fhit);
		if(commdepth==0)
			escapeWrite((void*)flastok, fhit-flastok, mfout);
		flastok=(const char*)(fend+3);
		mfSetLength(mfbuf, 0);

		mfwrite((void*)(fhit+5), 1, fend-(fhit+5), mfbuf);
		parseMeta(mfout, mfGetData(mfbuf));
	}
	searchInput(flastok, (char*)0x8FFFFFFF);
	escapeWrite((void*)flastok, strlen(flastok), mfout);
	mfprintf(mfout, "\";\n");
	
	mfclose(mfin);
	return(mfout);
}
/*
 * ntp_readline_init - setup, set or reset prompt string
 */
int
ntp_readline_init(
	const char *	prompt
	)
{
	int	success;

	success = 1;

	if (prompt) {
		if (lineedit_prompt) 
			free(lineedit_prompt);
		lineedit_prompt = estrdup(prompt);
	}

#ifdef LE_EDITLINE
	if (NULL == ntp_el) {

# if 4 == EL_INIT_ARGS
		ntp_el = el_init(progname, stdin, stdout, stderr);
# else
		ntp_el = el_init(progname, stdin, stdout);
# endif
		if (ntp_el) {

			el_set(ntp_el, EL_PROMPT, ntp_prompt_callback);
			el_set(ntp_el, EL_EDITOR, "emacs");

			ntp_hist = history_init();

			if (NULL == ntp_hist) {

				mfprintf(stderr, "history_init(): %m\n");
				fflush(stderr);

				el_end(ntp_el);
				ntp_el = NULL;

				success = 0;

			} else {
				ZERO(hev);
#ifdef H_SETSIZE
				history(ntp_hist, &hev, H_SETSIZE, 128);
#endif
				el_set(ntp_el, EL_HIST, history,
				       ntp_hist);
				/* use any .editrc */
				el_source(ntp_el, NULL);
			}
		} else
			success = 0;
	}
#endif	/* LE_EDITLINE */

	ntp_readline_initted = success;

	return success;
}
Beispiel #6
0
/*************************************************************************
* This function writes a graphs into a file 
**************************************************************************/
void WriteGraph(char *filename, idxtype nvtxs, idxtype *xadj, idxtype *adjncy)
{
  idxtype i, j;
  FILE *fpout;

  fpout = gk_fopen(filename, "w", __func__);

  mfprintf(fpout, "%D %D", nvtxs, xadj[nvtxs]/2);

  for (i=0; i<nvtxs; i++) {
    mfprintf(fpout, "\n");
    for (j=xadj[i]; j<xadj[i+1]; j++)
      fprintf(fpout, " %" PRIIDX, adjncy[j]+1);
  }

  gk_fclose(fpout);
}
Beispiel #7
0
static int
alt_msgr(const char* file, int line, const char *format, va_list ap)
{
   if (file != NULL)
   {
      mfprintf (stderr, "I see a file...%s and a line, it's %d! ", file, line);
   }

   if (format != NULL)
   {
      mfprintf (stderr, "Uh, and there's a message for you: ");
      mvfprintf (stderr, format, ap);
   }

   mfprintf (stderr, "\n");

   return 1;
}
void parseSection(MFILE *mfout, const char *params)
{
	char *name=(char*)params, *comment=strchr(params, ' '), *cend;
	
	if(secakt!=NULL)
		mfprintf(mfout, "\n\";\n");

	if(comment!=NULL) {
		*comment=0;
		comment++;
		if(comment[0]!=0 && comment[0]!='\'')
			pexit("Section-Comment not started with ' - exiting\n", params);
		comment++;
		cend=strchr(comment, '\'');
		if(cend==NULL) pexit("Section-Comment not ended with ' - exiting\n", params);
		*cend=0;
	}
	
	secakt=listInsert(name, comment);
	mfprintf(mfout, "\nconst char %s[]=\"", name);
}
void parseSLink(MFILE *mfout, const char *params)
{
	Content *c;
	char *p=(char*)params, *comment, *cend;
	if(secakt==NULL) pexit("SLink: tried to insert, but no section found\n", params);
	
	if(*params!='\'') comment=(char*)strdup("");
	else{
		comment=(char*)(params+1);
		cend=strchr(comment, '\'');
		if(cend==NULL) pexit("SLink: comment not ended with ' - exiting\n", params);
		*cend=0; cend++;
		while(*cend==' ') cend++;
		p=cend;
	}
			
	c=listInsertContent(secakt->list, "", "***SCRIPT LINK***", comment);
	listInsertContent(c->sub, "s", "script", "Script Name");

	mfprintf(mfout, "%%s?");
	while((p=parseParam(mfout, c->sub, p, true))!=NULL)
		mfputc('&', mfout);
}
/* ------------------------ score_mat_write_gnuplot ----------------------
 * For Plotting an alignment traceback, e.g. with gnuplot, you need the
 * smat->mat scores. This function writes them into an ASCII file with
 * filename as param with output in each line:
 * 	i j value
 */
int
score_mat_write_gnuplot( const struct score_mat *smat, const char *fname, const char *protA, const char *protB ){
    const char *this_sub = "score_mat_write_gnuplot";
    float **mat = smat->mat;
    size_t i,j;

    FILE *fp;
    if ((fp = mfopen ( fname, "w", this_sub)) == NULL)
        return EXIT_FAILURE;

    if ( mfprintf ( fp , "%s%s%s%s%s\n","# Data from ", protA, ".pdb and ", protB ,".pdb") < 0)
        goto error;

    if ( mfprintf ( fp, "# %u entries per side \n", (unsigned int) smat->n_cols ) < 0)
        goto error;

    if ( mfprintf ( fp, "# Total entries %u\n", (unsigned int)(smat->n_rows * smat->n_cols) ) < 0)
        goto error;

    for (i = 0; i < smat->n_rows; i++ ){
        for (j = 0; j < smat->n_cols; j++ ){
     	    if ( mfprintf ( fp, "%d %d %f \n", i, j , mat[i][j] ) < 0)
                goto error;
     	}
     	if ( mfprintf ( fp, "\n") < 0)
            goto error;
    }
    if ( mfprintf ( fp, "\n" )  < 0)
        goto error;

    fclose (fp);
    return EXIT_SUCCESS;

    error:
        mperror (this_sub);
        err_printf (this_sub, "Failed writing to %s", fname);
        fclose (fp);
        return EXIT_FAILURE;
}
Beispiel #11
0
/* ---------------- lsq_fit  ----------------------------------
 * Least square fit routine to determine the rotation matrix to
 * superimpose coordinates r1 onto coordinates r2.
 * omega is a symmetric matrix in symmetric storage mode:
 * The element [i][j] is stored at position [i*(i+1)/2+j] with i>j
 * Thanks to Wilfred and Thomas Huber.
 */
static int
lsq_fit(const int nr_atoms, const struct RPoint *r1, const struct RPoint *r2,
        float R[3][3])
{

    int i, j, ii, jj, n;
    float U[3][3], det_U, sign_detU, sigma;
    float H[3][3], K[3][3];     /*, R[3][3]; */
    float omega[21], eve_omega[36], eva_omega[6];
    const float TINY = 1.e-10;
    const float SMALL = 1.e-5;
    const char *this_sub = "lsq_fit";

/* ----- CALCULATE THE MATRIX U AND ITS DETERMINANT ----- */
    for (i = 0; i < 3; i++)
            for (j = 0; j < 3; j++)
                U[i][j] = 0.0;

    for (n = 0; n < nr_atoms; n++) {
            U[0][0] += r1[n].x * r2[n].x;
            U[0][1] += r1[n].x * r2[n].y;
            U[0][2] += r1[n].x * r2[n].z;
            U[1][0] += r1[n].y * r2[n].x;
            U[1][1] += r1[n].y * r2[n].y;
            U[1][2] += r1[n].y * r2[n].z;
            U[2][0] += r1[n].z * r2[n].x;
            U[2][1] += r1[n].z * r2[n].y;
            U[2][2] += r1[n].z * r2[n].z;
    }

    det_U = U[0][0] * U[1][1] * U[2][2] + U[0][2] * U[1][0] * U[2][1] +
        U[0][1] * U[1][2] * U[2][0] - U[2][0] * U[1][1] * U[0][2] -
        U[2][2] * U[1][0] * U[0][1] - U[2][1] * U[1][2] * U[0][0];

    if (fabs(det_U) < TINY) {
            err_printf(this_sub, "determinant of U equal to zero\n");
            return EXIT_FAILURE;
    }

    sign_detU = det_U / fabs(det_U);    /* sign !!! */


/* ----- CONSTRUCT OMEGA, DIAGONALIZE IT AND DETERMINE H AND K --- */

    for (i = 0; i < 6; i++)
            for (j = i; j < 6; j++)
                omega[(j * (j + 1) / 2) + i] = 0.0;
    for (j = 3; j < 6; j++) {
            jj = j * (j + 1) / 2;
            for (i = 0; i < 3; i++) {
                ii = jj + i;
                omega[ii] = U[i][j - 3];
            }
    }

#ifdef DEBUG
    fprintf(stdout, "omega matrix:\n");
    for (i = 0; i < 21; i++)
            fprintf(stdout, STR(%SFO \ t), omega[i]);
    fprintf(stdout, "\n");
#endif

    i = 6;                      /* dimension of omega matrix */
    j = 0;                      /* both, eigenvalues and eigenvectors are calculated */
    eigen(omega, eve_omega, &i, &j);

    for (i = 0; i < 6; i++)
            eva_omega[i] = omega[i * (i + 1) / 2 + i];

#ifdef DEBUG
    fprintf(stdout, "Eigenvalues:\n");
    for (i = 0; i < 6; i++)
            fprintf(stdout, STR(%SFO \ t), eva_omega[i]);
    fprintf(stdout, "\n");
    ii = 0;
    fprintf(stdout, "Eigenvectors:\n");
    for (j = 0; j < 6; j++) {   /* ----- elements of eigenvector i ------ */
            for (i = 0; i < 6; i++)     /* ----  loop for eigenvectors !!! ----- */
               fprintf(stdout, STR(%SFO \ t), eve_omega[ii++]);
            fprintf(stdout, "\n");
    }

#endif


    if (det_U < 0.0) {
            if (fabs(eva_omega[1] - eva_omega[2]) < SMALL) {
                err_printf(this_sub,
                       "determinant of U < 0 && degenerated eigenvalues\n");
                return EXIT_FAILURE;
        }
    }

    for (i = 0; i < 3; i++){
           for (j = 0; j < 3; j++) {
               H[i][j] = M_SQRT2 * eve_omega[j * 6 + i];
               K[i][j] = M_SQRT2 * eve_omega[j * 6 + i + 3];
       }
    }
    sigma = (H[1][0] * H[2][1] - H[2][0] * H[1][1]) * H[0][2] +
        (H[2][0] * H[0][1] - H[0][0] * H[2][1]) * H[1][2] +
        (H[0][0] * H[1][1] - H[1][0] * H[0][1]) * H[2][2];

    if (sigma <= 0.0) {
        for (i = 0; i < 3; i++) {
                H[i][2] = -H[i][2];
                K[i][2] = -K[i][2];
            }
    }

/* --------- DETERMINE R AND ROTATE X ----------- */
    for (i = 0; i < 3; i++)
           for (j = 0; j < 3; j++)
               R[j][i] = K[j][0] * H[i][0] + K[j][1] * H[i][1] +
           sign_detU * K[j][2] * H[i][2];


#ifdef DEBUG
    mfprintf(stdout, "Rotation matrix:\n");
    for (j = 0; j < 3; j++) {
           for (i = 0; i < 3; i++)
               mfprintf(stdout, "%f ", R[i][j]);
           mfprintf(stdout, "\n");
    }
#endif

    return EXIT_SUCCESS;
}
Beispiel #12
0
/*
** DNS Callback:
** - For each IP:
** - - open a socket
** - - increment n_pending_ntp
** - - send a request if this is a Unicast callback
** - - queue wait for response
** - decrement n_pending_dns
*/
void
sntp_name_resolved(
	int			rescode,
	int			gai_errno,
	void *			context,
	const char *		name,
	const char *		service,
	const struct addrinfo *	hints,
	const struct addrinfo *	addr
	)
{
	struct dns_ctx *	dctx;
	sent_pkt *		spkt;
	const struct addrinfo *	ai;
	SOCKET			sock;
	u_int			xmt_delay_v4;
	u_int			xmt_delay_v6;
	u_int			xmt_delay;
	size_t			octets;

	xmt_delay_v4 = 0;
	xmt_delay_v6 = 0;
	dctx = context;
	if (rescode) {
#ifdef EAI_SYSTEM
		if (EAI_SYSTEM == rescode) {
			errno = gai_errno;
			mfprintf(stderr, "%s lookup error %m\n",
				 dctx->name);
		} else
#endif
			fprintf(stderr, "%s lookup error %s\n",
				dctx->name, gai_strerror(rescode));
	} else {
		TRACE(3, ("%s [%s]\n", dctx->name,
			  (addr->ai_canonname != NULL)
			      ? addr->ai_canonname
			      : ""));

		for (ai = addr; ai != NULL; ai = ai->ai_next) {

			if (check_kod(ai))
				continue;

			switch (ai->ai_family) {

			case AF_INET:
				sock = sock4;
				xmt_delay = xmt_delay_v4;
				xmt_delay_v4++;
				break;

			case AF_INET6:
				if (!ipv6_works)
					continue;

				sock = sock6;
				xmt_delay = xmt_delay_v6;
				xmt_delay_v6++;
				break;

			default:
				msyslog(LOG_ERR, "sntp_name_resolved: unexpected ai_family: %d",
					ai->ai_family);
				exit(1);
				break;
			}

			/*
			** We're waiting for a response for either unicast
			** or broadcast, so...
			*/
			++n_pending_ntp;

			/* If this is for a unicast IP, queue a request */
			if (dctx->flags & CTX_UCST) {
				spkt = emalloc_zero(sizeof(*spkt));
				spkt->dctx = dctx;
				octets = min(ai->ai_addrlen, sizeof(spkt->addr));
				memcpy(&spkt->addr, ai->ai_addr, octets);
				queue_xmt(sock, dctx, spkt, xmt_delay);
			}
		}
	}
	/* n_pending_dns really should be >0 here... */
	--n_pending_dns;
	check_exit_conditions();
}
void Parse_XML_Online (int rank, xmlDocPtr xmldoc, xmlNodePtr current_tag)
{
  xmlNodePtr tag;

  tag = current_tag;

  xmlChar *analysis  = xmlGetProp (tag, RC_ONLINE_TYPE);
  xmlChar *frequency = xmlGetProp (tag, RC_ONLINE_FREQ);
  xmlChar *topology  = xmlGetProp (tag, RC_ONLINE_TOPO);

  /* Configure the type of analysis */
  if (analysis != NULL)
  {
    if (xmlStrcasecmp(analysis, RC_ONLINE_CLUSTERING) == 0)
    {
      Online_SetAnalysis(ONLINE_DO_CLUSTERING);
    }
    else if (xmlStrcasecmp(analysis, RC_ONLINE_SPECTRAL) == 0)
    {
      Online_SetAnalysis(ONLINE_DO_SPECTRAL);
    }
    else if (xmlStrcasecmp(analysis, RC_ONLINE_GREMLINS) == 0)
    {
      Online_SetAnalysis(ONLINE_DO_GREMLINS);
    }
    else
    {
      mfprintf(stderr, PACKAGE_NAME": XML Error: Value '%s' is not valid for property '<%s>'%s'\n",
        analysis, REMOTE_CONTROL_METHOD_ONLINE, RC_ONLINE_TYPE);
      exit(-1);
    }
  }

  /* Configure the frequency of the analysis */
  if (frequency != NULL)
  {
    Online_SetFrequencyString(frequency);
  }

  /* Configure the topology */
  if (topology != NULL)
  {
    Online_SetTopology((char *)topology);
  }
  XML_FREE(analysis);
  XML_FREE(frequency);

  tag = current_tag->xmlChildrenNode;
  while (tag != NULL)
  {
    if (!xmlStrcasecmp (tag->name, xmlTEXT) || !xmlStrcasecmp (tag->name, xmlCOMMENT)) { /* Skip coments */ }
#if defined(HAVE_CLUSTERING)
    else if (!xmlStrcasecmp (tag->name, RC_ONLINE_CLUSTERING))
    {
      xmlChar *clustering_config_str = xmlGetProp (tag, CLUSTERING_CONFIG);

      Online_SetClusteringConfig( (char *)clustering_config_str );

      XML_FREE(clustering_config_str);
    }
#endif
#if defined(HAVE_SPECTRAL)
    else if (!xmlStrcasecmp (tag->name, RC_ONLINE_SPECTRAL))
    {
      xmlChar *max_periods_str  = xmlGetProp (tag, SPECTRAL_MAX_PERIODS);
      xmlChar *num_iters_str    = xmlGetProp (tag, SPECTRAL_NUM_ITERS);
      xmlChar *min_seen_str     = xmlGetProp (tag, SPECTRAL_MIN_SEEN);
      xmlChar *min_likeness_str = xmlGetProp (tag, SPECTRAL_MIN_LIKENESS);
      int max_periods  = 0;

      if (max_periods_str != NULL)
      {
        if (strcmp((const char *)max_periods_str, "all") == 0) max_periods = 0;
        else max_periods = atoi((const char *)max_periods_str);
        Online_SetSpectralMaxPeriods ( max_periods );
      }
      if (num_iters_str    != NULL) 
      {
        Online_SetSpectralNumIters   ( atoi((const char *)num_iters_str) );
      }
      if (min_seen_str     != NULL) 
      {
        Online_SetSpectralMinSeen    ( atoi((const char *)min_seen_str) );
      }
      if (min_likeness_str != NULL) 
      {
        Online_SetSpectralMinLikeness( (atof((const char *)min_likeness_str) / 100.0) );
      }

      XML_FREE(max_periods_str);
      XML_FREE(num_iters_str);
      XML_FREE(min_seen_str);
      XML_FREE(min_likeness_str);

      Parse_XML_SpectralAdvanced(rank, xmldoc, tag->xmlChildrenNode);
    }
#endif
    else if (!xmlStrcasecmp (tag->name, RC_ONLINE_GREMLINS))
    {
      xmlChar *start_str     = xmlGetProp(tag, GREMLINS_START);
      xmlChar *increment_str = xmlGetProp(tag, GREMLINS_INCREMENT);
      xmlChar *roundtrip_str = xmlGetProp(tag, GREMLINS_ROUNDTRIP);
      xmlChar *loop_str      = xmlGetProp(tag, GREMLINS_LOOP);

      if (start_str != NULL)
      {
        Online_SetGremlinsStartCount ( atoi((const char *)start_str) );
      }
      if (increment_str != NULL)
      {
        Online_SetGremlinsIncrement ( atoi((const char *)increment_str) );
      }
      if (roundtrip_str != NULL)
      {
        if (strcmp((const char *)roundtrip_str, "yes") == 0)
        {
          Online_SetGremlinsRoundtrip ( 1 );
        }
      }
      if (loop_str != NULL)
      {
        if (strcmp((const char *)loop_str, "yes") == 0)
        {
          Online_SetGremlinsLoop( 1 );
        }
      }
    }
    tag = tag->next;
  }
}
Beispiel #14
0
int main(void)
{
   char** string;
   unsigned long i;
   unsigned long sample = 100000;
   unsigned long width = 10;
   size_t dim[] = {100000, 10};
   char test_text[] = "Hello World";

   /* test allocating */
   string = XMALLOC (sample * sizeof (*string));
   if (string == NULL)
   {
      mfprintf (stderr, "Failed to use xmalloc()\n");
      return EXIT_FAILURE;
   }

   for (i=0; i < sample; i++)
   {
      string[i] = XMALLOC (width * sizeof (**string));
      if (string[i] == NULL)
      {
         mfprintf (stderr, "Failed to use xmalloc()\n");
         return EXIT_FAILURE;
      }
   }

   for (i=0; i < sample; i++)
   {
      XFREE (string[i]);
   }

   XFREE (string);

   /* check 2D allocation */
   string = (char**) XMALLOC_2D(sample, width, sizeof (**string));
   if (string == NULL)
   {
      mfprintf (stderr, "Failed to use xmalloc_2d()\n");
      return EXIT_FAILURE;
   }

   for (i = 0; i < sample; i++)
   {
      string[i][0] = 'H';
   }

   XFREE_2D ((void**) string);

   /* doing 2D allocation with rnd function */
   string = (char**) XMALLOC_RND (sizeof (**string),
                                  sizeof (dim)/sizeof (*dim),
                                  dim);
   if (string == NULL)
   {
      mfprintf (stderr, "Failed to use xmalloc_rnd()\n");
      return EXIT_FAILURE;
   }

   for (i = 0; i < sample; i++)
   {
      string[i][0] = 'H';
   }

   XFREE_ND (sizeof (dim)/sizeof (*dim), (void**) string);

   /* doing 2D allocation using nd function */
   string = (char**) XMALLOC_ND (sizeof (**string), 2, dim[0], dim[1]);
   if (string == NULL)
   {
      mfprintf (stderr, "Failed to use xmalloc_nd()\n");
      return EXIT_FAILURE;
   }

   for (i = 0; i < sample; i++)
   {
      string[i][0] = 'H';
   }

   XFREE_ND (2, (void**) string);

   /* trying to allocate an 2D array to store text (char*) */
   string = (char**) XMALLOC_ND (sizeof (*string), 1, dim[0]);
   if (string == NULL)
   {
      mfprintf (stderr, "Failed to use xmalloc_nd()\n");
      return EXIT_FAILURE;
   }

   for (i = 0; i < sample; i++)
   {
      string[i] = test_text;
   }

   /* we may only free the pointer we allocated! */
   XFREE ((void*) string);

   FREE_MEMORY_MANAGER;

   return EXIT_SUCCESS;
}
Beispiel #15
0
/*************************************************************************
* Let the game begin
**************************************************************************/
int main(int argc, char *argv[])
{
  idxtype i, j, istep, options[10], nn, ne, fstep, lstep, nparts, nboxes, u[3], dim, nchanges, ncomm;
  char filename[256];
  idxtype *mien, *mrng, *part, *oldpart, *sflag, *bestdims, *fepart;
  double *mxyz, *bxyz;
  idxtype *xadj, *adjncy, *cntptr, *cntind;
  idxtype numflag = 0, wgtflag = 0, edgecut, etype=2;
  void *cinfo;
  FILE *fpin;
  long long int *ltmp;

  if (argc != 6) {
    mfprintf(stderr, "Usage: %s <nn> <ne> <fstep> <lstep> <nparts>\n", argv[0]);
    exit(0);
  }

  nn     = atoi(argv[1]);
  ne     = atoi(argv[2]);
  fstep  = atoi(argv[3]);
  lstep  = atoi(argv[4]);
  nparts = atoi(argv[5]);

  mprintf("Reading %s, nn: %D, ne: %D, fstep: %D, lstep: %D, nparts: %D\n", filename, nn, ne, fstep, lstep, nparts);

  mien = idxmalloc(4*ne, "main: mien");
  mxyz = gk_dmalloc(3*nn, "main: mxyz");
  mrng = idxmalloc(4*ne, "main: mrng");
  bxyz = gk_dmalloc(6*ne*4, "main: bxyz");

  fepart  = idxmalloc(nn, "main: fepart");
  part    = idxmalloc(nn, "main: part");
  oldpart = idxmalloc(nn, "main: oldpart");
  sflag   = idxmalloc(nn, "main: sflag");

  bestdims  = idxsmalloc(2*nparts, -1, "main: bestdims");

  xadj   = idxmalloc(nn+1, "main: xadj");
  adjncy = idxmalloc(50*nn, "main: adjncy");


  /*========================================================================
   * Read the initial mesh and setup the graph and contact information
   *========================================================================*/
  msprintf(filename, "mien.%04D", fstep);
  fpin = GKfopen(filename, "rb", "main: mien");
  fread(mien, sizeof(int), 4*ne, fpin);
  for (i=0; i<4*ne; i++)
    mien[i] = Flip_int32(mien[i]);
  GKfclose(fpin);

  msprintf(filename, "mxyz.%04D", fstep);
  fpin = GKfopen(filename, "rb", "main: mxyz");
  fread(mxyz, sizeof(double), 3*nn, fpin);
  for (i=0; i<3*nn; i++) {
    ltmp = (long long int *)(mxyz+i);
    *ltmp = Flip_int64(*ltmp);
  }
  GKfclose(fpin);
  mprintf("%e %e %e\n", mxyz[3*0+0], mxyz[3*0+1], mxyz[3*0+2]);

  msprintf(filename, "mrng.%04D", fstep);
  fpin = GKfopen(filename, "rb", "main: mrng");
  fread(mrng, sizeof(int), 4*ne, fpin);
  for (i=0; i<4*ne; i++)
    mrng[i] = Flip_int32(mrng[i]);
  GKfclose(fpin);


  /*========================================================================
   * Determine which nodes are in the surface
   *========================================================================*/
  iset(nn, 0, sflag);
  for (i=0; i<ne; i++) {
    if (mrng[4*i+0] > 0) { /* 1, 2, 3 */
      sflag[mien[4*i+0]-1] = 1;
      sflag[mien[4*i+1]-1] = 1;
      sflag[mien[4*i+2]-1] = 1;
    }
    if (mrng[4*i+1] > 0) { /* 1, 2, 4 */
      sflag[mien[4*i+0]-1] = 1;
      sflag[mien[4*i+1]-1] = 1;
      sflag[mien[4*i+3]-1] = 1;
    }
    if (mrng[4*i+2] > 0) { /* 2, 3, 4 */
      sflag[mien[4*i+1]-1] = 1;
      sflag[mien[4*i+2]-1] = 1;
      sflag[mien[4*i+3]-1] = 1;
    }
    if (mrng[4*i+3] > 0) { /* 1, 3, 4 */
      sflag[mien[4*i+0]-1] = 1;
      sflag[mien[4*i+2]-1] = 1;
      sflag[mien[4*i+3]-1] = 1;
    }
  }

  mprintf("Contact Nodes: %D of %D\n", isum(nn, sflag), nn);


  /*========================================================================
   * Compute the FE partition
   *========================================================================*/
  numflag = mien[idxargmin(4*ne, mien)];
  METIS_MeshToNodal(&ne, &nn, mien, &etype, &numflag, xadj, adjncy);

  options[0] = 0;
  METIS_PartGraphVKway(&nn, xadj, adjncy, NULL, NULL, &wgtflag, &numflag, &nparts,
        options, &edgecut, fepart);

  mprintf("K-way partitioning Volume: %D\n", edgecut);


  /*========================================================================
   * Get into the loop in which you go over the different configurations
   *========================================================================*/
  for (istep=fstep; istep<=lstep; istep++) {
    msprintf(filename, "mxyz.%04D", istep);
    mprintf("Reading %s...............................................................\n", filename);
    fpin = GKfopen(filename, "rb", "main: mxyz");
    fread(mxyz, sizeof(double), 3*nn, fpin);
    for (i=0; i<3*nn; i++) {
      ltmp = (long long int *)(mxyz+i);
      *ltmp = Flip_int64(*ltmp);
    }
    GKfclose(fpin);

    msprintf(filename, "mrng.%04D", istep);
    fpin = GKfopen(filename, "rb", "main: mrng");
    fread(mrng, sizeof(int), 4*ne, fpin);
    for (i=0; i<4*ne; i++)
      mrng[i] = Flip_int32(mrng[i]);
    GKfclose(fpin);

    /* Determine which nodes are in the surface */
    iset(nn, 0, sflag);
    for (i=0; i<ne; i++) {
      if (mrng[4*i+0] > 0) { /* 1, 2, 3 */
        sflag[mien[4*i+0]-1] = 1;
        sflag[mien[4*i+1]-1] = 1;
        sflag[mien[4*i+2]-1] = 1;
      }
      if (mrng[4*i+1] > 0) { /* 1, 2, 4 */
        sflag[mien[4*i+0]-1] = 1;
        sflag[mien[4*i+1]-1] = 1;
        sflag[mien[4*i+3]-1] = 1;
      }
      if (mrng[4*i+2] > 0) { /* 2, 3, 4 */
        sflag[mien[4*i+1]-1] = 1;
        sflag[mien[4*i+2]-1] = 1;
        sflag[mien[4*i+3]-1] = 1;
      }
      if (mrng[4*i+3] > 0) { /* 1, 3, 4 */
        sflag[mien[4*i+0]-1] = 1;
        sflag[mien[4*i+2]-1] = 1;
        sflag[mien[4*i+3]-1] = 1;
      }
    }

    mprintf("Contact Nodes: %D of %D\n", isum(nn, sflag), nn);

    /* Determine the bounding boxes of the surface elements */
    for (nboxes=0, i=0; i<ne; i++) {
      if (mrng[4*i+0] > 0) { /* 1, 2, 3 */
        u[0] = mien[4*i+0]-1;
        u[1] = mien[4*i+1]-1;
        u[2] = mien[4*i+2]-1;
        bxyz[6*nboxes+0] = bxyz[6*nboxes+3] = mxyz[3*u[0]+0];
        bxyz[6*nboxes+1] = bxyz[6*nboxes+4] = mxyz[3*u[0]+1];
        bxyz[6*nboxes+2] = bxyz[6*nboxes+5] = mxyz[3*u[0]+2];
        for (j=1; j<3; j++) {
          for (dim=0; dim<3; dim++) {
            bxyz[6*nboxes+dim] = (bxyz[6*nboxes+dim] > mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+dim]);
            bxyz[6*nboxes+3+dim] = (bxyz[6*nboxes+3+dim] < mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+3+dim]);
          }
        }
        nboxes++;
      }
      if (mrng[4*i+1] > 0) { /* 1, 2, 4 */
        u[0] = mien[4*i+0]-1;
        u[1] = mien[4*i+1]-1;
        u[2] = mien[4*i+3]-1;
        bxyz[6*nboxes+0] = bxyz[6*nboxes+3] = mxyz[3*u[0]+0];
        bxyz[6*nboxes+1] = bxyz[6*nboxes+4] = mxyz[3*u[0]+1];
        bxyz[6*nboxes+2] = bxyz[6*nboxes+5] = mxyz[3*u[0]+2];
        for (j=1; j<3; j++) {
          for (dim=0; dim<3; dim++) {
            bxyz[6*nboxes+dim] = (bxyz[6*nboxes+dim] > mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+dim]);
            bxyz[6*nboxes+3+dim] = (bxyz[6*nboxes+3+dim] < mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+3+dim]);
          }
        }
        nboxes++;
      }
      if (mrng[4*i+2] > 0) { /* 2, 3, 4 */
        u[0] = mien[4*i+1]-1;
        u[1] = mien[4*i+2]-1;
        u[2] = mien[4*i+3]-1;
        bxyz[6*nboxes+0] = bxyz[6*nboxes+3] = mxyz[3*u[0]+0];
        bxyz[6*nboxes+1] = bxyz[6*nboxes+4] = mxyz[3*u[0]+1];
        bxyz[6*nboxes+2] = bxyz[6*nboxes+5] = mxyz[3*u[0]+2];
        for (j=1; j<3; j++) {
          for (dim=0; dim<3; dim++) {
            bxyz[6*nboxes+dim] = (bxyz[6*nboxes+dim] > mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+dim]);
            bxyz[6*nboxes+3+dim] = (bxyz[6*nboxes+3+dim] < mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+3+dim]);
          }
        }
        nboxes++;
      }
      if (mrng[4*i+3] > 0) { /* 1, 3, 4 */
        u[0] = mien[4*i+0]-1;
        u[1] = mien[4*i+2]-1;
        u[2] = mien[4*i+3]-1;
        bxyz[6*nboxes+0] = bxyz[6*nboxes+3] = mxyz[3*u[0]+0];
        bxyz[6*nboxes+1] = bxyz[6*nboxes+4] = mxyz[3*u[0]+1];
        bxyz[6*nboxes+2] = bxyz[6*nboxes+5] = mxyz[3*u[0]+2];
        for (j=1; j<3; j++) {
          for (dim=0; dim<3; dim++) {
            bxyz[6*nboxes+dim] = (bxyz[6*nboxes+dim] > mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+dim]);
            bxyz[6*nboxes+3+dim] = (bxyz[6*nboxes+3+dim] < mxyz[3*u[j]+dim] ? mxyz[3*u[j]+dim] : bxyz[6*nboxes+3+dim]);
          }
        }
        nboxes++;
      }
    }

    cinfo = METIS_PartSurfForContactRCB(&nn, mxyz, sflag, &nparts, part, bestdims);

    METIS_FindContacts(cinfo, &nboxes, bxyz, &nparts, &cntptr, &cntind);

    METIS_FreeContactInfo(cinfo);

    nchanges = 0;
    if (istep > fstep) {
      for (i=0; i<nn; i++)
        nchanges += (part[i] != oldpart[i] ? 1 : 0);
    }
    idxcopy(nn, part, oldpart);

    ncomm = ComputeMapCost(nn, nparts, fepart, part);

    mprintf("Contacting Elements: %D  Indices: %D  Nchanges: %D  MapCost: %D\n", nboxes, cntptr[nboxes]-nboxes, nchanges, ncomm);

    gk_free((void **)&cntptr, &cntind, LTERM);
  }

}  
Beispiel #16
0
static int
simulate_using_nn_scoring (struct brot_args_info* brot_args,
                           SeqMatrix* sm,
                           Scmf_Rna_Opt_data* data,
                           GFile* entropy_file)
{
   int error = 0;
   char** bp_allowed = NULL;
   char bi, bj;
   unsigned long i, j, k;
   unsigned long alpha_size
      = alphabet_size (scmf_rna_opt_data_get_alphabet(data));
   unsigned long allowed_bp = 0;
   NN_scores* scores =
      NN_SCORES_NEW_INIT(50.0f, scmf_rna_opt_data_get_alphabet (data));

   if (scores == NULL)
   {
      error = 1;
   }

   /* prepare index of allowed base pairs */
   if (!error)
   {
      /* "randomise" scoring function */
      mfprintf (stdout, "Using seed: %ld\n", brot_args->seed_arg);
      nn_scores_add_thermal_noise (alpha_size, brot_args->seed_arg, scores);

      bp_allowed = XMALLOC(alpha_size * sizeof (*bp_allowed));
      if (bp_allowed == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {
      allowed_bp = nn_scores_no_allowed_basepairs (scores);

      /* we need 1 byte for each possible pair + 1byte for the NULL byte for
         each letter in the alphabet */
      bp_allowed[0] = XCALLOC (allowed_bp + alpha_size,
                               sizeof (**(bp_allowed)));
      if (bp_allowed[0] == NULL)
      {
         error = 1;
      }
   }

   if (!error)
   {
      i = 0;
      k = 0;
      while (i < alpha_size)
      {
         bp_allowed[i] = bp_allowed[0] + (k * sizeof (**(bp_allowed)));
         
         for (j = 0; j < allowed_bp; j++)
         {
            nn_scores_get_allowed_basepair (j, &bi, &bj, scores);
            if (i == (unsigned) bi)
            {
               bp_allowed[0][k] = bj + 1;
               k++;
            }
         }
         
         k++;
         i++;
      }
   }

   /* decompose secondary structure */
   if (!error)
   {
      error = scmf_rna_opt_data_secstruct_init (data);
   }

   mfprintf (stdout, "TREATMENT of fixed sites: If both sites of a pair are "
             "fixed, delete from list? Verbose info!!!\n");

   /* set our special function for calc. cols.: Iterate over sec.struct., not
      sequence matrix! */
   if (!error)
   {
      scmf_rna_opt_data_set_scores (scores, data);
      scmf_rna_opt_data_set_bp_allowed (bp_allowed, data);
      scmf_rna_opt_data_set_scales (brot_args->negative_design_scaling_arg,
                                    brot_args->heterogenity_term_scaling_arg,
                                    data);
      scmf_rna_opt_data_set_het_window (brot_args->window_size_arg, data);

      seqmatrix_set_func_calc_eeff_col (scmf_rna_opt_calc_col_nn, sm);
      seqmatrix_set_gas_constant (8.314472, sm);

      error = seqmatrix_simulate_scmf (brot_args->steps_arg,
                                       brot_args->temp_arg,
                                       brot_args->beta_long_arg,
                                       brot_args->beta_short_arg,
                                       brot_args->speedup_threshold_arg,
                                       brot_args->min_cool_arg,
                                       brot_args->scale_cool_arg,
                                       brot_args->lambda_arg,
                                       brot_args->sm_entropy_arg,
                                       entropy_file,
                                       sm,
                                       data);
   }

   /* collate */
   if (!error)
   {
     /*  seqmatrix_print_2_stdout (2, sm); */
      seqmatrix_set_transform_row (scmf_rna_opt_data_transform_row_2_base, sm);

      error = seqmatrix_collate_is (0.99,
                                    brot_args->steps_arg / 2,
                                    brot_args->temp_arg,
                                    brot_args->beta_long_arg,
                                    brot_args->beta_short_arg,
                                    brot_args->speedup_threshold_arg,
                                    brot_args->min_cool_arg,
                                    brot_args->scale_cool_arg,
                                    brot_args->lambda_arg,
                                    brot_args->sm_entropy_arg,
                                    sm,
                                    data);
      /*error = seqmatrix_collate_mv (sm, data);*/
   }

   /* first: iterate scmf on secstruct, not sm! */

   nn_scores_delete (scores);
   scmf_rna_opt_data_set_scores (NULL, data);
   scmf_rna_opt_data_set_bp_allowed (NULL, data);

   XFREE (bp_allowed[0]);
   XFREE (bp_allowed);

   return error;
}
Beispiel #17
0
int main(int argc,char *argv[])
{
   int retval;
   unsigned long len;

   if (argc > 1)
   {
      mfprintf (stderr, "%s called with an argument.\n", argv[0]);      
      return EXIT_FAILURE;
   }

   /* test program name */
   retval = set_progname (argv[0]);
   if (retval != 0)
   {
      mfprintf (stderr, "Failed to init messenger: %s.\n", argv[0]);      
      return EXIT_FAILURE;      
   }

   retval = add_name_2_progname (argv[0]);
   if (retval != 0)
   {
      mfprintf (stderr, "Failed to add name to program name: %s.\n", argv[0]);
      return EXIT_FAILURE;      
   }

   len = mprintf ("%s\n", get_progname());
   if (len != ((strlen (argv[0]) * 2) + 2))
   {
      mfprintf (stderr, "Failed to get program name: %s.\n", argv[0]);
      return EXIT_FAILURE;      
   }

   len = get_progname_len();
   if (len != ((strlen (argv[0]) * 2) + 1))
   {
      mfprintf (stderr, "Failed to get program name length: %s.\n", argv[0]);
      return EXIT_FAILURE;      
   }

   if (THROW_ERROR_MSG ("%s is %d test. Don%ct worry!", "This", 1, '\'') < 0)
   {
      mfprintf (stderr, "Failed to print error message: %s\n", argv[0]);
      return EXIT_FAILURE;
   }

   set_error_msg_func (alt_msgr);
   THROW_ERROR_MSG ("Hello chap!");

   if (THROW_WARN_MSG ("Now testing %d %s.", 1, "Warning") < 0)
   {
      mfprintf (stderr, "Failed to print error message: %s\n", argv[0]);
      return EXIT_FAILURE;
   }


   set_warn_msg_func (alt_msgr);
   THROW_WARN_MSG ("Hello bloke!");

   free_progname();

   FREE_MEMORY_MANAGER;

   return EXIT_SUCCESS;
}