Esempio n. 1
0
/*! \brief Destroy distributed L & U matrices. */
void
Destroy_LU(int_t n, gridinfo_t *grid, LUstruct_t *LUstruct)
{
    int_t i, nb, nsupers;
    Glu_persist_t *Glu_persist = LUstruct->Glu_persist;
    LocalLU_t *Llu = LUstruct->Llu;

#if ( DEBUGlevel>=1 )
    int iam;
    MPI_Comm_rank( MPI_COMM_WORLD, &iam );
    CHECK_MALLOC(iam, "Enter Destroy_LU()");
#endif

    nsupers = Glu_persist->supno[n-1] + 1;

    nb = CEILING(nsupers, grid->npcol);
    for (i = 0; i < nb; ++i) 
	if ( Llu->Lrowind_bc_ptr[i] ) {
	    SUPERLU_FREE (Llu->Lrowind_bc_ptr[i]);
#ifdef GPU_ACC
	    checkCuda(cudaFreeHost(Llu->Lnzval_bc_ptr[i]));
#else
	    SUPERLU_FREE (Llu->Lnzval_bc_ptr[i]);
#endif
	}
    SUPERLU_FREE (Llu->Lrowind_bc_ptr);
    SUPERLU_FREE (Llu->Lnzval_bc_ptr);

    nb = CEILING(nsupers, grid->nprow);
    for (i = 0; i < nb; ++i)
	if ( Llu->Ufstnz_br_ptr[i] ) {
	    SUPERLU_FREE (Llu->Ufstnz_br_ptr[i]);
	    SUPERLU_FREE (Llu->Unzval_br_ptr[i]);
	}
    SUPERLU_FREE (Llu->Ufstnz_br_ptr);
    SUPERLU_FREE (Llu->Unzval_br_ptr);

    /* The following can be freed after factorization. */
    SUPERLU_FREE(Llu->ToRecv);
    SUPERLU_FREE(Llu->ToSendD);
    SUPERLU_FREE(Llu->ToSendR[0]);
    SUPERLU_FREE(Llu->ToSendR);

    /* The following can be freed only after iterative refinement. */
    SUPERLU_FREE(Llu->ilsum);
    SUPERLU_FREE(Llu->fmod);
    SUPERLU_FREE(Llu->fsendx_plist[0]);
    SUPERLU_FREE(Llu->fsendx_plist);
    SUPERLU_FREE(Llu->bmod);
    SUPERLU_FREE(Llu->bsendx_plist[0]);
    SUPERLU_FREE(Llu->bsendx_plist);
    SUPERLU_FREE(Llu->mod_bit);

    SUPERLU_FREE(Glu_persist->xsup);
    SUPERLU_FREE(Glu_persist->supno);

#if ( DEBUGlevel>=1 )
    CHECK_MALLOC(iam, "Exit Destroy_LU()");
#endif
}
Esempio n. 2
0
/* solve natural resistance problem in FT version
 * for both periodic and non-periodic boundary conditions
 * INPUT
 *  sys : system parameters
 *  u [np * 3] : in the labo frame.
 *  o [np * 3] : in the labo frame.
 * OUTPUT
 *  f [np * 3] :
 *  t [np * 3] :
 */
void
solve_res_3ft_matrix (struct stokes * sys,
		      const double *u, const double *o,
		      double *f, double *t)
{
  if (sys->version != 1)
    {
      fprintf (stderr, "libstokes solve_res_3ft_matrix :"
	       " the version is wrong. reset to FT\n");
      sys->version = 1;
    }

  int np = sys->np;
  double *u0 = (double *) malloc (sizeof (double) * np * 3);
  double *o0 = (double *) malloc (sizeof (double) * np * 3);
  CHECK_MALLOC (u0, "solve_res_3ft_matrix");
  CHECK_MALLOC (o0, "solve_res_3ft_matrix");

  shift_labo_to_rest_U (sys, np, u, u0);
  shift_labo_to_rest_O (sys, np, o, o0);
  /* the main calculation is done in the the fluid-rest frame;
   * u(x)=0 as |x|-> infty */

  solve_res_3ft_matrix_0 (sys,
			  u0, o0, 
			  f, t);

  free (u0);
  free (o0);

  /* for the interface, we are in the labo frame, that is
   * u(x) is given by the imposed flow field as |x|-> infty */
  // here, no velocity in output, we do nothing
}
Esempio n. 3
0
/*
* strtab_create:  Return pointer to strtab; to be passed to strtab_insert()
*	and strtab_free().
*/
STRTAB *
strtab_create(void)
{
    STRTAB *strtab;

    /*
       * Set alignSize to one less thatn the least power of 2 greater or
       * equal to IDSIZE.
       * This is assumed to be the maximum required alignment for this type.
     */
    if (alignSize == 0) {	/* first time only */
	alignSize = 1;
	while (alignSize < IDSIZE)
	    alignSize <<= 1;
	--alignSize;
    }

    strtab = (STRTAB *) malloc(sizeof *strtab);
    CHECK_MALLOC(strtab);

    strtab->buf_list = NULL;
    strtab->buf_ptr = NULL;
    strtab->size = 0;
    strtab->index = (TABLE *) table_create(strcmp);
    if (strtab->index == NULL) {
	free(strtab);
	return (NULL);
    }
    strtab->upfix = (void *) upfix_init(UPFIX_MAX_LEN, NULL);
    CHECK_MALLOC(strtab->upfix);

    return strtab;
}
Esempio n. 4
0
/* wrapper of fastSI_f() for GSL-MULTROOT routine
 */
int
fastSI_GSL_MULTIROOT_func (const gsl_vector *x, void *p,
			   gsl_vector *f)
{
  struct BD_imp *b = (struct BD_imp *)p;

  int np3 = 3 * b->BD->sys->np;
  double *xcur = (double *)malloc (sizeof (double) * np3);
  double *fcur = (double *)malloc (sizeof (double) * np3);
  CHECK_MALLOC (xcur, "fastSI_GSL_MULTIROOT_func");
  CHECK_MALLOC (fcur, "fastSI_GSL_MULTIROOT_func");

  int i;
  for (i = 0; i < np3; i ++)
    {
      xcur[i] = gsl_vector_get (x, i);
    }

  fastSI_f (b, xcur, fcur);

  for (i = 0; i < np3; i ++)
    {
      gsl_vector_set (f, i, fcur[i]);
    }

  free (xcur);
  free (fcur);

  return (GSL_SUCCESS);
}
Esempio n. 5
0
struct cfile *
cf_openOut(const char *path)
{
    struct cfile *cf;

    cf = (struct cfile *) malloc(sizeof *cf);
    CHECK_MALLOC(cf);

    cf->fp = fopen(path, "w");
    if (cf->fp == NULL) {
	free(cf);
	return NULL;
    }

    cf->fileName = (char *) malloc(strlen(path) + 1);
    CHECK_MALLOC(cf->fileName);
    strcpy(cf->fileName, path);

    cf->mode = W_MODE;		/* read */
    cf->lineNo = 0;
    cf->pendingCount = 0;
    cf->pendingValue = -1;
    cf->atFirstChar = 1;

    return cf;
}
Esempio n. 6
0
/* initialize struct EV_LJ
 * INPUT
 *  np     : number of particles
 * OUTPUT
 *  returned value : struct EV_LJ,
 *      where LJ parameters are set by zero.
 */
struct EV_LJ *
EV_LJ_init (int np)
{
  struct EV_LJ *ev_LJ = (struct EV_LJ *)malloc (sizeof (struct EV_LJ));
  CHECK_MALLOC (ev_LJ, "EV_LJ_init");

  ev_LJ->n    = np;
  ev_LJ->flag = (int *)malloc (sizeof (int) * np);
  ev_LJ->e    = (double *)malloc (sizeof (double) * np);
  ev_LJ->r0   = (double *)malloc (sizeof (double) * np);
  CHECK_MALLOC (ev_LJ->flag, "EV_LJ_init");
  CHECK_MALLOC (ev_LJ->e,    "EV_LJ_init");
  CHECK_MALLOC (ev_LJ->r0,   "EV_LJ_init");

  // zero clear
  int i;
  for (i = 0; i < np; i ++)
    {
      ev_LJ->flag[i] = 0;
      ev_LJ->e   [i] = 0.0;
      ev_LJ->r0  [i] = 0.0;
    }

  return (ev_LJ);
}
Esempio n. 7
0
void
BONDS_GROUP_set (struct BONDS_GROUP *g,
		 int np,
		 const int *ip,
		 const int *bonds)
{
  if (np == 0) return;

  g->np = np;
  g->ip = (int *)malloc (sizeof (int) * np);
  CHECK_MALLOC (g->ip, "BONDS_GROUP_set");
  int i;
  for (i = 0; i < np; i ++)
    {
      g->ip[i] = ip[i];
    }

  if (np == 1)
    {
      g->bonds = NULL;
    }
  else
    {
      g->bonds = (int *)malloc (sizeof (int) * (np-1));
      CHECK_MALLOC (g->bonds, "BONDS_GROUP_set");
    }
  for (i = 0; i < np-1; i ++)
    {
      g->bonds[i] = bonds[i];
    }
}
Esempio n. 8
0
/* check KIrand_Gaussian()
 */
int
check_KIrand_Gaussian (int verbose, double tiny)
{
  if (verbose != 0)
    {
      fprintf (stdout,
	       "==================================================\n"
	       "check_KIrand_Gaussian : start\n");
    }

  int check = 0;
  double max = 0.0;

  int n = 1000000;
  double *x0 = (double *)malloc (sizeof (double) * n);
  CHECK_MALLOC (x0, "check_KIrand_Gaussian");

  int i;
  unsigned long seed = 0;
  struct KIrand *rng = KIrand_init ();
  CHECK_MALLOC (rng, "check_KIrand_Gaussian");
  KIrand_init_genrand (rng, seed);
  for (i = 0; i < n; i ++)
    {
      x0[i] = KIrand_Gaussian (rng);
    }
  KIrand_free (rng);

  // check for Gaussian properties
  double mean = 0.0;
  for (i = 0; i < n; i ++)
    {
      mean += x0[i];
    }
  mean /= (double)n;
  double vari = 0.0;
  for (i = 0; i < n; i ++)
    {
      vari += (x0[i] - mean) * (x0[i] - mean);
    }
  vari /= (double)n;

  check += compare_max (mean+1.0, 1.0, " mean", verbose, tiny, &max);
  check += compare_max (vari,     1.0, " vari", verbose, tiny, &max);

  free (x0);


  if (verbose != 0)
    {
      fprintf (stdout, " max error = %e vs tiny = %e\n", max, tiny);
      if (check == 0) fprintf (stdout, " => PASSED\n\n");
      else            fprintf (stdout, " => FAILED\n\n");
    }

  return (check);
}
Esempio n. 9
0
/* solve natural resistance problem in FT version in the fluid-rest frame
 * for both periodic and non-periodic boundary conditions
 * INPUT
 *  sys : system parameters
 *  u [np * 3] : = U - u^inf, that is, in the fluid-rest frame
 *  o [np * 3] : = O - O^inf, that is, in the fluid-rest frame
 * OUTPUT
 *  f [np * 3] :
 *  t [np * 3] :
 */
void
solve_res_lub_3ft_matrix_0 (struct stokes * sys,
			    const double *u, const double *o,
			    double *f, double *t)
{
  if (sys->version != 1)
    {
      fprintf (stderr, "libstokes solve_res_lub_3ft_matrix_0 :"
	       " the version is wrong. reset to FT\n");
      sys->version = 1;
    }

  int np = sys->np;
  int n6 = np * 6;

  double *mob = (double *) malloc (sizeof (double) * n6 * n6);
  double *lub = (double *) malloc (sizeof (double) * n6 * n6);
  double *b = (double *) malloc (sizeof (double) * n6);
  double *x = (double *) malloc (sizeof (double) * n6);
  CHECK_MALLOC (mob, "solve_res_lub_3ft_matrix");
  CHECK_MALLOC (lub, "solve_res_lub_3ft_matrix");
  CHECK_MALLOC (b, "solve_res_lub_3ft_matrix");
  CHECK_MALLOC (x, "solve_res_lub_3ft_matrix");

  // M matrix
  make_matrix_mob_3all (sys, mob); // sys->version is 1 (FT)
  // M^-1
  lapack_inv_ (n6, mob);

  // L matrix
  make_matrix_lub_3ft (sys, lub);

  // M^-1 + L
  int i;
  for (i = 0; i < n6 * n6; i ++)
    {
      lub [i] += mob [i];
    }
  free (mob);

  /* b := (UO) */
  set_ft_by_FT (np, b, u, o);

  // x := (M^-1 + L).(UO)
  dot_prod_matrix (lub, n6, n6, b, x);

  // (FT) = x
  set_FT_by_ft (np, f, t, x);

  free (lub);
  free (b);
  free (x);
}
int fd_dictfct_Address_encode(void * data, union avp_value * avp_value)
{
	sSS * ss = (sSS *) data;
	uint16_t AddressType = 0;
	size_t	size = 0;
	unsigned char * buf = NULL;
	
	TRACE_ENTRY("%p %p", data, avp_value);
	CHECK_PARAMS( data && avp_value  );
	
	switch (ss->ss_family) {
		case AF_INET:
			{
				/* We are encoding an IP address */
				sSA4 * sin = (sSA4 *)ss;
				
				AddressType = 1;/* see http://www.iana.org/assignments/address-family-numbers/ */
				size = 6;	/* 2 for AddressType + 4 for data */
				
				CHECK_MALLOC(  buf = malloc(size)  );
				
				/* may not work because of alignment: *(uint32_t *)(buf+2) = htonl(sin->sin_addr.s_addr); */
				memcpy(buf + 2, &sin->sin_addr.s_addr, 4);
			}
			break;
				
		case AF_INET6:
			{
				/* We are encoding an IPv6 address */
				sSA6 * sin6 = (sSA6 *)ss;
				
				AddressType = 2;/* see http://www.iana.org/assignments/address-family-numbers/ */
				size = 18;	/* 2 for AddressType + 16 for data */
				
				CHECK_MALLOC(  buf = malloc(size)  );
				
				/* The order is already good here */
				memcpy(buf + 2, &sin6->sin6_addr.s6_addr, 16);
				
			}
			break;
				
		default:
			CHECK_PARAMS( AddressType = 0 );
	}
	
	*(uint16_t *)buf = htons(AddressType);

	avp_value->os.len = size;
	avp_value->os.data = buf;
	
	return 0;
}
Esempio n. 11
0
/* solve natural mobility problem with fixed particles in FT version
 * without HI
 * for both periodic and non-periodic boundary conditions
 * INPUT
 *  sys : system parameters
 *  f [nm * 3] :
 *  t [nm * 3] :
 *  uf [nf * 3] :
 *  of [nf * 3] :
 * OUTPUT
 *  u [nm * 3] :
 *  o [nm * 3] :
 *  ff [nf * 3] :
 *  tf [nf * 3] :
 */
void
solve_mix_3ft_noHI (struct stokes *sys,
		    const double *f, const double *t,
		    const double *uf, const double *of,
		    double *u, double *o,
		    double *ff, double *tf)
{
  if (sys->version != 1)
    {
      fprintf (stderr, "libstokes solve_mix_3ft_noHI :"
	       " the version is wrong. reset to FT\n");
      sys->version = 1;
    }

  int np = sys->np;
  int nm = sys->nm;
  int i;

  // for fixed particles
  int nf = np - nm;
  if (nf > 0)
    {
      double *uf0 = (double *) malloc (sizeof (double) * nf * 3);
      double *of0 = (double *) malloc (sizeof (double) * nf * 3);
      CHECK_MALLOC (uf0, "solve_mix_3ft_noHI");
      CHECK_MALLOC (of0, "solve_mix_3ft_noHI");

      shift_labo_to_rest_U (sys, nf, uf, uf0);
      shift_labo_to_rest_O (sys, nf, of, of0);
      /* the main calculation is done in the the fluid-rest frame;
       * u(x)=0 as |x|-> infty */
      for (i = 0; i < nf * 3; i ++)
	{
	  ff[i] = uf0[i];
	  tf[i] = of0[i] / 0.75;
	}
      free (uf0);
      free (of0);
    }

  // for mobile particles
  for (i = 0; i < nm * 3; i ++)
    {
      u[i] = f[i];
      o[i] = t[i] * 0.75;
    }
  /* for the interface, we are in the labo frame, that is
   * u(x) is given by the imposed flow field as |x|-> infty */
  shift_rest_to_labo_U (sys, nm, u);
  shift_rest_to_labo_O (sys, nm, o);
}
Esempio n. 12
0
int handle_server_failure(struct jsonrpc_server *server)
{
	LM_INFO("Setting timer to reconnect to %s on port %d in %d seconds.\n", server->host, server->port, JSONRPC_RECONNECT_INTERVAL);

	if (server->socket)
		close(server->socket);
	server->socket = 0;
	if (server->ev != NULL) {
		event_del(server->ev);
		pkg_free(server->ev);
		server->ev = NULL;
	}
	server->status = JSONRPC_SERVER_FAILURE;
	server->conn_attempts--;
	if(_jsonrpcc_max_conn_retry!=-1 && server->conn_attempts<0) {
		LM_ERR("max reconnect attempts. No further attempts will be made to reconnect this server.");
		return -1;
	}

	int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
	
	if (timerfd == -1) {
		LM_ERR("Could not create timerfd to reschedule connection. No further attempts will be made to reconnect this server.");
		return -1;
	}

	struct itimerspec *itime = pkg_malloc(sizeof(struct itimerspec));
	CHECK_MALLOC(itime);
	itime->it_interval.tv_sec = 0;
	itime->it_interval.tv_nsec = 0;
	
	itime->it_value.tv_sec = JSONRPC_RECONNECT_INTERVAL;
	itime->it_value.tv_nsec = 0;
	
	if (timerfd_settime(timerfd, 0, itime, NULL) == -1) 
	{
		LM_ERR("Could not set timer to reschedule connection. No further attempts will be made to reconnect this server.");
		return -1;
	}
	LM_INFO("timerfd value is %d\n", timerfd);
	struct event *timer_ev = pkg_malloc(sizeof(struct event));
	CHECK_MALLOC(timer_ev);
	event_set(timer_ev, timerfd, EV_READ, reconnect_cb, server); 
	if(event_add(timer_ev, NULL) == -1) {
		LM_ERR("event_add failed while rescheduling connection (%s). No further attempts will be made to reconnect this server.", strerror(errno));
		return -1;
	}
	server->ev = timer_ev;
	server->timer = itime;
	return 0;
}
Esempio n. 13
0
/* solve generalized linear set of equations using LU-decomposition
 * INPUT
 *  n1, n2 : dimension
 *  A [n1 * n1] :
 *  B [n1 * n2] :
 *  C [n2 * n1] :
 *  D [n2 * n2] :
 *
 *  E [n1 * n1] :
 *  F [n1 * n2] :
 *  G [n2 * n1] :
 *  H [n2 * n2] :
 *  where the generalized linear set of equations is
 *   [A B](x) = [E F](b)
 *   [C D](y)   [G H](c)
 * OUTPUT
 *  I [n1 * n1] :
 *  J [n1 * n2] :
 *  K [n2 * n1] :
 *  L [n2 * n2] :
 *  where the generalized linear set of equations is
 *   (x) = [I J](b)
 *   (c)   [K L](y)
 *  note that A-D, E-H are destroyed!
 */
void
solve_gen_linear (int n1, int n2,
		  double * A, double * B, double * C, double * D,
		  double * E, double * F, double * G, double * H,
		  double * I, double * J, double * K, double * L)
{
  /* H := H^-1 */
  lapack_inv_ (n2, H);

  /* C := (H^-1) . C */
  mul_left_sq (C, n2, n1, H);
  /* G := (H^-1) . G */
  mul_left_sq (G, n2, n1, H);
  /* D := (H^-1) . D */
  mul_left_sq (D, n2, n2, H);


  double *a = (double *)malloc (sizeof (double) * n1 * n1);
  double *e = (double *)malloc (sizeof (double) * n1 * n1);
  double *b = (double *)malloc (sizeof (double) * n1 * n2);
  CHECK_MALLOC (a, "solve_gen_linear");
  CHECK_MALLOC (e, "solve_gen_linear");
  CHECK_MALLOC (b, "solve_gen_linear");

  /* a [n1, n1] := A - F.(H^-1).C */
  add_and_mul (A, n1, n1, F, n1, n2, C, n2, n1, 1.0, -1.0, a);
  // e [n1, n1] := E - F.(H^-1).G
  add_and_mul (E, n1, n1, F, n1, n2, G, n2, n1, 1.0, -1.0, e);
  // b [n1, n2] := - B + F.(H^-1).D
  add_and_mul (B, n1, n2, F, n1, n2, D, n2, n2, -1.0, 1.0, b);

  /* a := (A-F.(H^-1).C)^-1 */
  lapack_inv_ (n1, a);

  /* I := a.e */
  mul_matrices (a, n1, n1, e, n1, n1, I);
  /* J := a.b */
  mul_matrices (a, n1, n1, b, n1, n2, J);

  free (a);
  free (e);
  free (b);


  // K := - G + C.I
  add_and_mul (G, n2, n1, C, n2, n1, I, n1, n1, -1.0, 1.0, K);
  // L := D + C.J
  add_and_mul (D, n2, n2, C, n2, n1, J, n1, n2, 1.0, 1.0, L);
}
Esempio n. 14
0
struct pv_complex *
pv_complex_init (long len, long hop_syn, int flag_window)
{
   int i;
   struct pv_complex * pv
      = (struct pv_complex *) malloc (sizeof (struct pv_complex));
   CHECK_MALLOC (pv, "pv_complex_init");

   pv->len = len;
   pv->hop_syn = hop_syn;

   pv->flag_window = flag_window;

   pv->window_scale = get_scale_factor_for_window (len, hop_syn, flag_window);

   pv->time = (double *)fftw_malloc (len * sizeof(double));
   pv->freq = (double *)fftw_malloc (len * sizeof(double));
   CHECK_MALLOC (pv->time, "pv_complex_init");
   CHECK_MALLOC (pv->freq, "pv_complex_init");
   pv->plan = fftw_plan_r2r_1d (len, pv->time, pv->freq,
                                FFTW_R2HC, FFTW_ESTIMATE);

   pv->f_out = (double *)fftw_malloc (len * sizeof(double));
   pv->t_out = (double *)fftw_malloc (len * sizeof(double));
   CHECK_MALLOC (pv->f_out, "pv_complex_init");
   CHECK_MALLOC (pv->t_out, "pv_complex_init");
   pv->plan_inv = fftw_plan_r2r_1d (len, pv->f_out, pv->t_out,
                                    FFTW_HC2R, FFTW_ESTIMATE);

   pv->l_f_old = (double *)malloc (len * sizeof(double));
   pv->r_f_old = (double *)malloc (len * sizeof(double));
   CHECK_MALLOC (pv->l_f_old, "pv_complex_init");
   CHECK_MALLOC (pv->r_f_old, "pv_complex_init");

   pv->l_out = (double *) malloc ((hop_syn + len) * sizeof(double));
   pv->r_out = (double *) malloc ((hop_syn + len) * sizeof(double));
   CHECK_MALLOC (pv->l_out, "pv_complex_init");
   CHECK_MALLOC (pv->r_out, "pv_complex_init");
   for (i = 0; i < (hop_syn + len); i ++)
   {
      pv->l_out [i] = 0.0;
      pv->r_out [i] = 0.0;
   }

   pv->flag_left  = 0; /* l_f_old[] is not initialized yet */
   pv->flag_right = 0; /* r_f_old[] is not initialized yet */

   pv->flag_lock = 0; /* no phase lock (for default) */

   /*pv->pitch_shift = 0.0; // no pitch-shift */

   return (pv);
}
Esempio n. 15
0
File: snd.c Progetto: Red54/WaoN
long sndfile_write (SNDFILE *sf, SF_INFO sfinfo,
		    double * left, double * right,
		    int len)
{
  sf_count_t status;

  if (sfinfo.channels == 1)
    {
      status = sf_writef_double (sf, left, (sf_count_t)len);
    }
  else
    {
      double *buf = NULL;
      buf = (double *) malloc (sizeof (double) * len * sfinfo.channels);
      CHECK_MALLOC (buf, "sndfile_write");
      int i;
      for (i = 0; i < len; i ++)
	{
	  buf [i * sfinfo.channels]     = left  [i];
	  buf [i * sfinfo.channels + 1] = right [i];
	}
      status = sf_writef_double (sf, buf, (sf_count_t)len);
      free (buf);
    }

  return ((long) status);
}
Esempio n. 16
0
/* Register a new hook callback */
int fd_hook_register (  uint32_t type_mask,
                        void (*fd_hook_cb)(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata),
                        void  *regdata,
                        struct fd_hook_data_hdl *data_hdl,
                        struct fd_hook_hdl ** handler )
{
    struct fd_hook_hdl * newhdl = NULL;
    int i;

    TRACE_ENTRY("%x %p %p %p %p", type_mask, fd_hook_cb, regdata, data_hdl, handler);

    CHECK_PARAMS( fd_hook_cb && handler );

    CHECK_MALLOC( newhdl = malloc(sizeof(struct fd_hook_hdl)) );
    memset(newhdl, 0, sizeof(struct fd_hook_hdl));

    newhdl->fd_hook_cb = fd_hook_cb;
    newhdl->regdata = regdata;
    newhdl->data_hdl = data_hdl;

    for (i=0; i <= HOOK_LAST; i++) {
        fd_list_init(&newhdl->chain[i], newhdl);
        if (type_mask & (1<<i)) {
            CHECK_POSIX( pthread_rwlock_wrlock(&HS_array[i].rwlock) );
            fd_list_insert_before( &HS_array[i].sentinel, &newhdl->chain[i]);
            CHECK_POSIX( pthread_rwlock_unlock(&HS_array[i].rwlock) );
        }
    }

    *handler = newhdl;
    return 0;
}
Esempio n. 17
0
/*! \brief Deallocate LUstruct */
void LUstructFree(LUstruct_t *LUstruct)
{
#if ( DEBUGlevel>=1 )
    int iam;
    MPI_Comm_rank( MPI_COMM_WORLD, &iam );
    CHECK_MALLOC(iam, "Enter LUstructFree()");
#endif

    SUPERLU_FREE(LUstruct->etree);
    SUPERLU_FREE(LUstruct->Glu_persist);
    SUPERLU_FREE(LUstruct->Llu);

#if ( DEBUGlevel>=1 )
    CHECK_MALLOC(iam, "Exit LUstructFree()");
#endif
}
Esempio n. 18
0
/*
 * Starts SELECT on a Flexilite class
 */
static int _open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor)
{
    int result;

    struct flexi_ClassDef_t *vtab = (struct flexi_ClassDef_t *) pVTab;
    // Cursor will have 2 prepared sqlite statements: 1) find object IDs by property values (either with index or not), 2) to iterate through found objects' properties
    struct flexi_VTabCursor *cur = NULL;
    CHECK_MALLOC(cur, sizeof(struct flexi_VTabCursor));

    *ppCursor = (void *) cur;
    memset(cur, 0, sizeof(*cur));

    cur->iEof = -1;
    cur->lObjectID = -1;

    const char *zPropSql = "select ObjectID, PropertyID, PropIndex, ctlv, [Value] from [.ref-values] "
            "where ObjectID = :1 order by ObjectID, PropertyID, PropIndex;";

    // TODO
    //    CHECK_STMT_PREPARE(vtab->pCtx->db, zPropSql, &cur->pPropertyIterator);

    result = SQLITE_OK;
    goto EXIT;

    ONERROR:
    printf("%s", sqlite3_errmsg(vtab->pCtx->db));

    EXIT:
    return result;
}
Esempio n. 19
0
/* C := alpha*op( A )*op( B ) + beta*C
 * INPUT
 *  m : the number of rows   of the matrix op(A),C => op(A)[ij],i=0~m.
 *  n : the number of colums of the matrix op(B),C => op(B)[ij],j=0~n.
 *  k : the number of colums of the matrix op(A) and
 *      the number of rows   of the matrix op(B)   => op(A)[ij],j=0~k.
 *      that is, C[m,n] = alpha A[m,k] . B[k,n] + beta * C[m,n]
 *  op(A) is 'm by k' matrix => op(A)[m,k]
 *  op(B)    'k by n' matrix => op(B)[k,n]
 *     C     'm by n' matrix =>    C [m,n],
 *            where n is the number of columns (colum means 'tate-block')
 *            and   m is the number of rows.   (row   means 'line')
 *  dgemm_() is implemented in FORTRAN, so that,
 *  for 'N' case,
 *     C[i,j] = sum_l A[i,l] B[l,j]
 *            = sum_L a[I+m*L] * b[L+k*J], where I:=i-1, etc.
 *            = sum_L a[L*m+I] * b[J*k+L]
 *            = A_C[L,I] * B_C[J,L]
 *  for 'T' case,
 *     C[i,j] = sum_l A[l,i] B[j,l]
 *            = sum_L a[L+k*I] * b[J+n*L] where I:=i-1, etc.
 *            = sum_L a[I*k+L] * b[L*n+J]
 *            = A_C[I,L] * B_C[L,J]
 *  for both cases,
 *     C[i,j] = c[I+m*J] = C[J*m+I] = C_C[J,I]
 * SUMMARY in C:
 *   for 'N' : C[I,J] = A[L,J] * B[I,L]
 *   for 'T' : C[I,J] = A[J,L] * B[L,I]
 * here, call dgemm_() in 'T' mode, and then, transpose the result.
 */
void dgemm_wrap (int m, int n, int k,
		 double alpha, double *a, double *b,
		 double beta, double *c)
{
  char trans = 'T'; /* fortran's memory allocation is transposed */
  double *d = (double *)malloc (sizeof (double) * m*n);
  CHECK_MALLOC (d, "dgemm_wrap");

  int i, j;
  for (i = 0; i < m; i ++)
    {
      for (j = 0; j < n; j ++)
	{
	  d[j*m+i] = c[i*n+j];
	}
    }

  dgemm_ (&trans, &trans, &m, &n, &k,
	  &alpha, a, &k, b, &n,
	  &beta, d, &m);

  for (i = 0; i < m; i ++)
    {
      for (j = 0; j < n; j ++)
	{
	  c[i*n+j] = d[j*m+i];
	}
    }

  free (d);
}
Esempio n. 20
0
/* add a group into struct BONDS_GROUPS
 * INPUT
 *  gs         : struct BONDS_GROUPS
 *  np         : number of particles for the group
 *  ip[np]     : particle-index list for the group, with which x of 
 *               the particle i is accessed by pos[ip[i]*3]
 *  bonds[np-1]: bond-index list for the group, with which particles 
 *               of i-th bond is accessed by ia[bonds[i]] and ib[bonds[i]]
 * OUTPUT
 *  gs
 */
void
BONDS_GROUPS_add (struct BONDS_GROUPS *gs,
		  int np, const int *ip, const int *bonds)
{
  gs->n ++;
  gs->group
    = (struct BONDS_GROUP **)realloc (gs->group,
				      sizeof (struct BONDS_GROUP *) * gs->n);
  CHECK_MALLOC (gs->group, "BONDS_GROUPS_add");

  int n = gs->n - 1;
  gs->group[n] = BONDS_GROUP_init ();
  CHECK_MALLOC (gs->group[n], "BONDS_GROUPS_add");

  BONDS_GROUP_set (gs->group[n], np, ip, bonds);
}
Esempio n. 21
0
/**
 * @brief Compute the reverse complement.
 * @param str The master string.
 * @param len The length of the master string
 * @return The reverse complement. The caller has to free it!
 */
char *revcomp(const char *str, size_t len) {
	if (!str) return NULL;
	char *rev = malloc(len + 1);
	CHECK_MALLOC(rev);

	char *r = rev;
	const char *s = &str[len - 1];
	rev[len] = '\0';

	do {
		char d;

		switch (*s--) {
			case 'A': d = 'T'; break;
			case 'T': d = 'A'; break;
			case 'G': d = 'C'; break;
			case 'C': d = 'G'; break;
			case '!': d = ';'; break; // rosebud
			default: continue;
		}

		*r++ = d;
	} while (--len);

	return rev;
}
Esempio n. 22
0
/* assign one particle into RYUON_grid
 * INPUT
 *  g : struct RYUON_grid
 *  x[3] : position of the particle
 * OUTPUT
 *  (returned value) : 0 (false) == the particle is out of range
 *                     1 (true)  == the particle is assigned successfully
 *  g :
 */
int
GRID_add_particle (struct RYUON_grid *g,
		   const double *x,
		   int ip)
{
  int ix = (int)floor ((x[0] - g->x0) / g->lx);
  int iy = (int)floor ((x[1] - g->y0) / g->ly);
  int iz = (int)floor ((x[2] - g->z0) / g->lz);
  if (ix < 0 || ix >= g->nx ||
      iy < 0 || iy >= g->ny ||
      iz < 0 || iz >= g->nz)
    {
      fprintf (stderr, "# (%f, %f, %f) => (%d %d %d)\n",
	       x[0], x[1], x[2], ix, iy, iz);
      return (0); // false
    }

  int in = GRID_ixyz_to_in (g, ix, iy, iz);

  g->np[in] ++;
  g->ip[in] = (int *)realloc (g->ip[in], sizeof (int) * g->np[in]);
  CHECK_MALLOC (g->ip[in], "GRID_add_particle");

  g->ip[in][g->np[in] - 1] = ip;

  return (1); // true
}
Esempio n. 23
0
/* Function called when a new RADIUS message is being converted to Diameter */
static int debug_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
{
	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
	
	fd_log_debug("------------- RADIUS/Diameter Request Debug%s%s%s -------------", cs ? " [" : "", cs ? (char *)cs : "", cs ? "]" : "");
	
	if (!rad_req) {
		fd_log_debug(" RADIUS request: NULL pointer");
	} else {
		fd_log_debug(" RADIUS request (%p) DUMP:", rad_req);
		debug_dump_radius(rad_req);
	}
	
	if (!rad_ans || ! *rad_ans) {
		fd_log_debug(" RADIUS answer: NULL pointer");
	} else {
		fd_log_debug(" RADIUS answer (%p) DUMP:", *rad_ans);
		debug_dump_radius(*rad_ans);
	}
	
	if (!diam_fw || ! *diam_fw) {
		fd_log_debug(" Diameter message: NULL pointer");
	} else {
		char * buf = NULL; size_t buflen;
		CHECK_MALLOC( fd_msg_dump_treeview(&buf, &buflen, NULL, *diam_fw, NULL, 0, 1) );
		fd_log_debug(" Diameter message (%p) DUMP: %s", *diam_fw, buf);
		free(buf);
	}
	
	fd_log_debug("===========  Debug%s%s%s complete =============", cs ? " [" : "", cs ? (char *)cs : "", cs ? "]" : "");
	
	return 0;
}
Esempio n. 24
0
/*! \brief Symmetric elimination tree
 *
 * <pre>
 *      p = spsymetree (A);
 *
 *      Find the elimination tree for symmetric matrix A.
 *      This uses Liu's algorithm, and runs in time O(nz*log n).
 *
 *      Input:
 *        Square sparse matrix A.  No check is made for symmetry;
 *        elements below and on the diagonal are ignored.
 *        Numeric values are ignored, so any explicit zeros are 
 *        treated as nonzero.
 *      Output:
 *        Integer array of parents representing the etree, with n
 *        meaning a root of the elimination forest.
 *      Note:  
 *        This routine uses only the upper triangle, while sparse
 *        Cholesky (as in spchol.c) uses only the lower.  Matlab's
 *        dense Cholesky uses only the upper.  This routine could
 *        be modified to use the lower triangle either by transposing
 *        the matrix or by traversing it by rows with auxiliary
 *        pointer and link arrays.
 *
 *      John R. Gilbert, Xerox, 10 Dec 1990
 *      Based on code by JRG dated 1987, 1988, and 1990.
 *      Modified by X.S. Li, November 1999.
 * </pre>
 */
int
sp_symetree_dist(
	    int_t *acolst, int_t *acolend, /* column starts and ends past 1 */
	    int_t *arow,            /* row indices of A */
	    int_t n,                /* dimension of A */
	    int_t *parent	    /* parent in elim tree */
	    )
{
	int_t	*root;		    /* root of subtee of etree 	*/
	int_t	rset, cset;             
	int_t	row, col;
	int_t	rroot;
	int_t	p;
	int_t   *pp;

#if ( DEBUGlevel>=1 )
	CHECK_MALLOC(0, "Enter sp_symetree()");
#endif

	root = mxCallocInt (n);
	initialize_disjoint_sets (n, &pp);

	for (col = 0; col < n; col++) {
		cset = make_set (col, pp);
		root[cset] = col;
		parent[col] = n; /* Matlab */
		for (p = acolst[col]; p < acolend[col]; p++) {
			row = arow[p];
			if (row >= col) continue;
			rset = find (row, pp);
			rroot = root[rset];
			if (rroot != col) {
				parent[rroot] = col;
				cset = link (cset, rset, pp);
				root[cset] = col;
			}
		}
	}
	SUPERLU_FREE (root);
	finalize_disjoint_sets (pp);

#if ( DEBUGlevel>=1 )
	CHECK_MALLOC(0, "Exit sp_symetree()");
#endif
	return 0;
} /* SP_SYMETREE_DIST */
Esempio n. 25
0
/* initialize RYUON_grid
 * by bounding box (x0,x1, y0,y1, z0,z1)
 * and cell number (nx, ny, nz)
 * NOTE: particles are not assigned yet.
 * use GRID_add_particle() or GRID_assign_particles().
 * alternatively, use GRID_init_all_by_l() in the first place.
 * INPUT
 *  x0,x1, y0,y1, z0,z1 : bounding box
 *  nx, ny, nz : cell number
 * OUTPUT
 *  (returned value) : struct RYUON_grid
 */
struct RYUON_grid *
GRID_init (double x0, double x1,
	   double y0, double y1,
	   double z0, double z1,
	   int nx, int ny, int nz)
{
  struct RYUON_grid *g
    = (struct RYUON_grid *)malloc (sizeof (struct RYUON_grid));
  CHECK_MALLOC (g, "GRID_init");

  g->x0 = x0;
  g->y0 = y0;
  g->z0 = z0;

  g->nx = nx;
  g->ny = ny;
  g->nz = nz;
  g->n  = nx * ny * nz;

  //double safe_factor = 1.0 + 1.0e-12;
  double safe_factor = 1.0 + 1.0e-15;
  g->lx = (x1 - x0) / (double)nx * safe_factor;
  g->ly = (y1 - y0) / (double)ny * safe_factor;
  g->lz = (z1 - z0) / (double)nz * safe_factor;


  // particle list for each cell
  g->np = (int *)calloc (sizeof (int), g->n);
  CHECK_MALLOC (g->np, "GRID_init");

  g->ip = (int **)malloc (sizeof (int *) * g->n);
  int i;
  for (i = 0; i < g->n; i ++)
    {
      g->ip[i] = NULL;
    }


  // nearest neighbor list
  g->nofp = 0;
  g->nnn = NULL;
  g->nnp = NULL;


  return (g);
}
Esempio n. 26
0
/* initialize RYUON_grid
 * by bounding box (x0,x1, y0,y1, z0,z1)
 * and cell size l
 * NOTE: particles are not assigned yet.
 * use GRID_add_particle() or GRID_assign_particles().
 * alternatively, use GRID_init_all_by_cutoff() in the first place.
 * INPUT
 *  x0,x1, y0,y1, z0,z1 : bounding box
 *  l : cell size
 * OUTPUT
 *  (returned value) : struct RYUON_grid
 */
struct RYUON_grid *
GRID_init_by_l (double x0, double x1,
		double y0, double y1,
		double z0, double z1,
		double l)
{
  struct RYUON_grid *g
    = (struct RYUON_grid *)malloc (sizeof (struct RYUON_grid));
  CHECK_MALLOC (g, "GRID_init_by_l");

  g->x0 = x0;
  g->y0 = y0;
  g->z0 = z0;

  g->lx = l;
  g->ly = l;
  g->lz = l;

  double safe_factor = 1.0 + 1.0e-15;
  g->nx = (int)(safe_factor * (x1 - x0) / l) + 1;
  g->ny = (int)(safe_factor * (y1 - y0) / l) + 1;
  g->nz = (int)(safe_factor * (z1 - z0) / l) + 1;
  g->n  = g->nx * g->ny * g->nz;


  // particle list for each cell
  g->np = (int *)calloc (sizeof (int), g->n);
  CHECK_MALLOC (g->np, "GRID_init_by_l");

  g->ip = (int **)malloc (sizeof (int *) * g->n);
  int i;
  for (i = 0; i < g->n; i ++)
    {
      g->ip[i] = NULL;
    }


  // nearest neighbor list
  g->nofp = 0;
  g->nnn = NULL;
  g->nnp = NULL;


  return (g);
}
Esempio n. 27
0
int tree_init(tree_s *baum, size_t size) {
	if (!baum || !size) return 1;
	*baum = (tree_s){};
	baum->pool = malloc(2 * size * sizeof(tree_node));
	CHECK_MALLOC(baum->pool);
	memset(baum->pool, 0, 2 * size * sizeof(tree_node));
	baum->size = size;
	return 0;
}
Esempio n. 28
0
/** @brief Initializes a sequences
 *
 * @returns 0 iff successful.
 */
int seq_init(seq_t *S, const char *seq, const char *name) {
	if (!S || !seq || !name) {
		return 1;
	}

	*S = (seq_t){.S = strdup(seq), .name = strdup(name)};

	CHECK_MALLOC(S->S);
	CHECK_MALLOC(S->name);

	normalize(S);

	// recalculate the length because `normalize` might have stripped some
	// characters.
	S->len = strlen(S->S);

	return 0;
}
Esempio n. 29
0
/* solve natural mobility problem in FT version
 * for both periodic and non-periodic boundary conditions
 * INPUT
 *  sys : system parameters
 *   f [np * 3] :
 *   t [np * 3] :
 * OUTPUT
 *   u [np * 3] :
 *   o [np * 3] :
 */
void
solve_mob_3ft_matrix (struct stokes * sys,
		      const double *f, const double *t,
		      double *u, double *o)
{
  if (sys->version != 1)
    {
      fprintf (stderr, "libstokes solve_mob_3ft_matrix :"
	       " the version is wrong. reset to FT\n");
      sys->version = 1;
    }

  int np = sys->np;
  int n6 = np * 6;

  double *mat = (double *)malloc (sizeof (double) * n6 * n6);
  double *b = (double *)malloc (sizeof (double) * n6);
  double *x = (double *)malloc (sizeof (double) * n6);
  CHECK_MALLOC (mat, "solve_mob_3ft_matrix");
  CHECK_MALLOC (b, "solve_mob_3ft_matrix");
  CHECK_MALLOC (x, "solve_mob_3ft_matrix");

  /* the main calculation is done in the the fluid-rest frame;
   * u(x)=0 as |x|-> infty */

  /* b := (FTE) */
  set_ft_by_FT (np, b, f, t);

  // M
  make_matrix_mob_3all (sys, mat); // sys->version is 1 (FT)
  // x = M.b
  dot_prod_matrix (mat, n6, n6, b, x);

  set_FT_by_ft (np, u, o, x);

  free (mat);
  free (b);
  free (x);

  /* for the interface, we are in the labo frame, that is
   * u(x) is given by the imposed flow field as |x|-> infty */
  shift_rest_to_labo_U (sys, sys->np, u);
  shift_rest_to_labo_O (sys, sys->np, o);
}
Esempio n. 30
0
/* initialize struct EV
 * INPUT
 *  length : characteristic length
 *           (in the same dimension for rd below, usually nm)
 *  peclet : peclet number
 *  rd     : Debye length (in the same dimension for a above, usually nm)
 *  T      : temperature in Kelvin.
 *  e      : dielectric constant of the solution (dimensionless number)
 *  eps    : to determine cut-off distance through eps = exp (-r_cutoff / rd) 
 *  np     : number of particles
 * OUTPUT
 *  returned value : struct EV_DH,
 *      where only the system parameters (a_sys, rd) are defined.
 *      nu[i] is zero cleared.
 */
struct EV_DH *
EV_DH_init (double length, double peclet,
	    double rd, double T, double e,
	    double eps,
	    int np)
{
  struct EV_DH *ev_dh = (struct EV_DH *)malloc (sizeof (struct EV_DH));
  CHECK_MALLOC (ev_dh, "EV_DH_init");

  // Boltzmann constant
  double kB = 1.3806504e-23; // [N m / K]
  // elementary charge
  double ec = 1.602176487e-19; // [C]
  // permittivity of vacuum
  double e0 = 8.8541878176e-12; // [F / m] = [C^2 / N m^2]

  double kT = kB * T; // [N m]
  double e2by4pie0 = ec * ec / (4.0 * M_PI * e * e0); // [N m^2]

  ev_dh->a_sys = e2by4pie0 / (peclet * kT * length); // dimensionless
  ev_dh->rd    = rd / length; // dimensionless

  // set dv_dh->r2, the cut-off distance squared
  ev_dh->r_cutoff = - ev_dh->rd * log (eps); // dimensionless cut-off length
  ev_dh->r2       = (ev_dh->r_cutoff) * (ev_dh->r_cutoff);

  ev_dh->n  = np;
  ev_dh->nu = (double *)malloc (sizeof (double) * np);
  CHECK_MALLOC (ev_dh->nu, "EV_DH_init");

  // zero clear
  int i;
  for (i = 0; i < np; i ++)
    {
      ev_dh->nu [i] = 0.0;
    }

  /* RYUON_grid */
  ev_dh->flag_grid = 0;
  ev_dh->grid = NULL;


  return (ev_dh);
}