コード例 #1
0
ファイル: sd_hist.c プロジェクト: pranavm19/sddekit
sd_hist *
sd_hist_new_default(uint32_t nd, uint32_t *vi, double *vd, double t0, double dt)
{
	sd_hist *hist;
	hist_data *h = NULL, zh = { 0 };
	if ((hist = sd_malloc(sizeof(sd_hist))) == NULL
		|| (h = hist->ptr = sd_malloc(sizeof(hist_data))) == NULL
		|| (*h = zh, (h->nd = nd) < 1)
		|| sd_util_uniqi(nd, vi, &(h->nu), &(h->uvi)) != SD_OK
		|| (h->del = sd_malloc(sizeof(double) * nd)) == NULL
		|| (h->vi = sd_malloc(sizeof(uint32_t) * nd)) == NULL
		|| (memcpy(h->del, vd, nd * sizeof(double)),
		    memcpy(h->vi, vi, nd * sizeof(uint32_t)),
			0)
		|| setup_buffer_structure(h, dt) != SD_OK
		|| (h->buf = sd_malloc(sizeof(double) * h->lim[h->nu])) == NULL
		)
	{
		if (hist != NULL) sd_free(hist);
		if (h != NULL)
		{
			if (h->buf!=NULL) sd_free(h->buf);
			if (h->uvi!=NULL) sd_free(h->uvi);
			if (h->del!=NULL) sd_free(h->del);
			if (h->vi!=NULL) sd_free(h->vi);
		}
		sd_err("number of delays < 1 or memory alloc failed.");
		return NULL;
	}
	h->dt = dt;
	h->t = t0;
	*hist = sd_hist_defaults;
	hist->ptr = h;
	return hist;
}
コード例 #2
0
ファイル: emc.c プロジェクト: AbheekG/sddekit
struct sd_sch *
sd_sch_new_emc(double time, double dt, double lam,
	       struct sd_sys *sys, struct sd_hist *hist, struct sd_rng *rng)
{
	struct data *d, z={0};
	uint32_t n_dim = sys->get_n_dim(sys)
	       , n_in  = sys->get_n_in(sys)
	       , n_out = sys->get_n_out(sys)
	       ;
	if ((d = sd_malloc(sizeof(struct data))) == NULL
	 || (*d=z, 0)
	 || (d->f=sd_malloc(sizeof(double)*n_dim))==NULL
	 || (d->g=sd_malloc(sizeof(double)*n_dim))==NULL
	 || (d->z=sd_malloc(sizeof(double)*n_dim))==NULL
	 || (d->eps=sd_malloc(sizeof(double)*n_dim))==NULL
     || sch_base_init(&d->base,
            time, dt,
            n_dim, n_in, n_out,
            sys, hist, rng, 
            emc_n_byte, emc_free, emc_copy, emc_apply, &sample) != SD_OK
	)
	{
		if (d->f!=NULL) sd_free(d->f);
		if (d->g!=NULL) sd_free(d->g);
		if (d->z!=NULL) sd_free(d->z);
		if (d->eps!=NULL) sd_free(d->eps);
		if (d != NULL) sd_free(d);
		sd_err("alloc for emc scheme failed.");
		return NULL;
	}
	d->first_call = true;
	d->lam = lam;
	d->base.sch.data = d;
	return &(d->base.sch);
}
コード例 #3
0
ファイル: sd_hist.c プロジェクト: pranavm19/sddekit
static sd_stat fill(sd_hist *hist, sd_hfill *hf)
{
	uint32_t i, j, ui, o, n, *vi;
	double *t;
	char *errmsg;
	hist_data *h = hist->ptr;
	errmsg = NULL;
	t = NULL;
	vi = NULL;

	n = h->lim[h->nu];
	if ((t = sd_malloc (sizeof(double) * n))==NULL ||
		(vi = sd_malloc (sizeof(uint32_t) * n))==NULL) {
		errmsg = "failed to allocate memory for evaluating hist fill.";
		goto end;
	}

	/* expand indices per buffer element */
	for (i=0; i<h->nu; i++)
	{
		ui = h->uvi[i];
		for (j=h->lim[i]; j<h->lim[i+1]; j++)
			vi[j] = ui;
	}
	
	/* evaluate time per buffer element */
	for (i=0; i<h->nu; i++)
	{
		for (j=0; j<(h->len[i]-1); j++)
		{
			o = h->pos[i]; 	   /* current position in buffer */
			o += j;            /* time step through buffer */
			o %= h->len[i];    /* wrap around on section length */
			o += h->lim[i];    /* offset to start of section */
			t[o] = h->t - j * h->dt;
		}
		j = h->len[i] - 1;
		o = h->pos[i]; 	   /* current position in buffer */
		o += j;            /* time step through buffer */
		o %= h->len[i];    /* wrap around on section length */
		o += h->lim[i];    /* offset to start of section */
		t[o] = h->t + h->dt; /* last point is next grid point */
	}

	if (hf->apply(hf, n, t, vi, h->buf) != SD_OK)
		errmsg = "history fill function failed.";

end:
	if (t!=NULL) sd_free(t);
	if (vi!=NULL) sd_free(vi);

	return errmsg == NULL ? SD_OK : SD_ERR;
}
コード例 #4
0
ファイル: sd_hist.c プロジェクト: pranavm19/sddekit
static sd_stat setup_buffer_structure(hist_data *h, double dt)
{
	uint32_t i, j, ui;
	double maxd;
	char *errmsg;
	/* alloc */
	if (
		(h->maxd = sd_malloc (sizeof(double) * h->nu))==NULL ||
		(h->lim = sd_malloc (sizeof(double) * (h->nu + 1)))==NULL ||
		(h->len = sd_malloc (sizeof(double) * h->nu))==NULL ||
		(h->pos = sd_malloc (sizeof(double) * h->nu))==NULL
	   ) {
		errmsg = "failed to alloc internal storage.";
		goto fail;
	}
	/* vi2i requires max(uvi) then filling in vi2i[ui]=i */
	h->maxvi = 0;
	for (i=0; i<h->nu; i++)
		if (h->uvi[i] > h->maxvi)
			h->maxvi = h->uvi[i];
	/* alloc */
	if ((h->vi2i = sd_malloc (sizeof(uint32_t) * (h->maxvi + 1)))==NULL) {
		errmsg = "failed to alloc internal storage.";
		goto fail;
	}
	/* set up vi2i, maxd len pos lim */
	for (i=0; i<h->nu; i++)
	{
		ui = h->uvi[i];
		h->vi2i[ui] = i;
		maxd = 0.0;
		for (j=0; j<h->nd; j++)
			if (h->vi[j]==ui && h->del[j]>maxd)
				maxd = h->del[j];
		h->maxd[i] = maxd;
		h->len[i] = (uint32_t) ceil(maxd / dt) + 2;
		h->pos[i] = 0;
		if (i==0)
			h->lim[i] = 0;
		else
			h->lim[i] = h->lim[i-1] + h->len[i-1];
	}
	h->lim[h->nu] = h->lim[h->nu-1] + h->len[h->nu-1];
	return SD_OK;
fail:
	if (h->maxd!=NULL) sd_free(h->maxd);
	if (h->lim!=NULL) sd_free(h->lim);
	if (h->len!=NULL) sd_free(h->len);
	if (h->pos!=NULL) sd_free(h->pos);
	if (h->vi2i!=NULL) sd_free(h->vi2i);
	sd_err(errmsg);
	return SD_ERR;
}
コード例 #5
0
ファイル: sd_test.c プロジェクト: i-Zaak/sddekit
static char *cpstr(char *str)
{
	uint32_t nc = strlen(str) + 1;
	char *out = sd_malloc(sizeof(char) * nc);
	memcpy(out, str, sizeof(char) * nc);
	return out;
}
コード例 #6
0
ファイル: cb.c プロジェクト: Vio8023/sddekit
struct sd_sch *
sd_sch_new_cb(
	double time, double dt,
	struct sd_sys *sys,
	struct sd_hist *hist,
	struct sd_rng *rng,
	void *user_data,
	enum sd_stat(*user_apply)(void *))
{
	struct data *data;
	if ((data = sd_malloc(sizeof(struct data))) == NULL)
	{
		sd_err("alloc sch cb failed.");
		return NULL;
	}
	sch_base_init(&data->base,
		time, dt,
		sys->get_n_dim(sys),
		sys->get_n_in(sys),
		sys->get_n_out(sys),
		sys, hist, rng,
		&n_byte, &cb_free, &copy, &apply);
	data->base.sch.data = data;
	data->user_data = user_data;
	data->user_apply = user_apply;
	return &data->base.sch;
}
コード例 #7
0
ファイル: rww.c プロジェクト: AbheekG/sddekit
static struct data *
data_copy(struct data *data)
{
    struct data *copy = sd_malloc(sizeof(struct data));
    if (copy == NULL)
        sd_err("copy rww failed.");
    *copy = *data;
    return copy;
}
コード例 #8
0
ファイル: rng.c プロジェクト: AbheekG/sddekit
static struct data *data_copy(struct data *data)
{
	struct data *copy = sd_malloc(sizeof(struct data));
	if (copy == NULL)
	{
		sd_err("alloc rng copy failed.");
		return NULL;
	}
	memcpy(copy, data, sizeof(struct data));
	return copy;
}
コード例 #9
0
int main()
{
	int i, j, n, m;
	double *g, *z;
	time_t tic, toc;
	sd_rng *rng = sd_rng_new_default();

	n = 10000;
	m = 10;
	rng->seed(rng, 42);

	g = sd_malloc (sizeof(double) * n);
	z = sd_malloc (sizeof(double) * n);
	for (i=1; i<n; i+=100)
	{
		for (j=0; j<n; j++) g[i] = 0.0;

		/* put i nnz into g */
		for (j=0; j<i; j++) g[i] = 1.0;

		tic = clock();
		for (j=0; j<n; j++)
			if (g[j]!=0.0)
				g[j] *= rng->norm(rng);
		toc = clock();

		printf("i=%03d %.3fs\n", i, (double) (toc - tic) / CLOCKS_PER_SEC);

	}

	tic = clock();
	rng->fill_norm(rng, n, z);
	for (j=0; j<n; j++)
		g[j] *= z[j];
	toc = clock();
	printf("fill & mult %.3fs\n", (double) (toc - tic) / CLOCKS_PER_SEC);

	return 0;
}
コード例 #10
0
ファイル: conn.c プロジェクト: AbheekG/sddekit
struct sd_conn * sd_conn_new_sparse(
	uint32_t n_rows,
	uint32_t n_cols,
	uint32_t n_nonzero,
	uint32_t *row_offsets,
	uint32_t *col_indices,
	double *weights,
	double *delays
)
{
	struct conn *conn, zero={0};
	if ((conn = sd_malloc(sizeof(struct conn))) == NULL
	 || (*conn = zero, 0)
	 || (conn->row_offsets = sd_malloc(sizeof(uint32_t)*(n_rows + 1))) == NULL
	 || (conn->col_indices = sd_malloc(sizeof(uint32_t)*n_nonzero)) == NULL
	 || (conn->weights = sd_malloc(sizeof(double)*n_nonzero)) == NULL
	 || (conn->delays = sd_malloc(sizeof(double)*n_nonzero)) == NULL
	)
	{
		if (conn->row_offsets!=NULL) sd_free(conn->row_offsets);
		if (conn->col_indices!=NULL) sd_free(conn->col_indices);
		if (conn->weights!=NULL) sd_free(conn->weights);
		if (conn!=NULL) sd_free(conn);
		sd_err("alloc conn data failed.");
		return NULL;
	}
	conn->n_row = n_rows;
	conn->n_col = n_cols;
	conn->nnz = n_nonzero;
	memcpy(conn->row_offsets, row_offsets, sizeof(uint32_t) * n_nonzero);
	memcpy(conn->col_indices, col_indices, sizeof(uint32_t) * n_nonzero);
	memcpy(conn->weights, weights, sizeof(double) * n_nonzero);
	memcpy(conn->delays, delays, sizeof(double) * n_nonzero);
	conn->delay_scale = 1.0;
	conn->sd_conn = sd_conn_defaults;
	conn->sd_conn.data = conn;
	return &(conn->sd_conn);
}
コード例 #11
0
ファイル: rww.c プロジェクト: AbheekG/sddekit
struct sd_sys_rww *sd_sys_rww_new()
{
	struct data *data;
	if ((data = sd_malloc(sizeof(struct data)))==NULL)
	{
		sd_err("alloc sys rww failed.");
		return NULL;
	}
    data->pars = pars_defaults;
    data->sd_sys = sd_sys_defaults;
    data->sd_sys_rww = sd_sys_rww_defaults;
	data->sd_sys.data = data->sd_sys_rww.data = data;
	return &data->sd_sys_rww;
}
コード例 #12
0
ファイル: until.c プロジェクト: Vio8023/sddekit
SD_API struct sd_out *
sd_out_new_until(double time)
{
	struct data *data, zero = {0};
	if ((data = sd_malloc(sizeof(struct data))) == NULL)
	{
		sd_err("alloc out until failed.");
		return NULL;
	}
	data->time = time;
	data->sd_out = sd_out_defaults;
	data->sd_out.data = data;
	return &(data->sd_out);
}
コード例 #13
0
ファイル: rng.c プロジェクト: AbheekG/sddekit
struct sd_rng *
sd_rng_new_mt(uint32_t seed)
{
	struct data *data;
	if ((data = sd_malloc(sizeof(struct data))) == NULL)
	{
		sd_err("alloc rng failed.");
		return NULL;
	}
	data->seed = seed;
	data->sd_rng = sd_rng_defaults;
	data->sd_rng.data = data;
	sd_rng_mt_seed(seed, &data->rks);
	return &data->sd_rng;
}
コード例 #14
0
ファイル: solv.c プロジェクト: AbheekG/sddekit
struct sd_sol *
sd_sol_new_default(double init_time, double *init_state,
		   struct sd_sch *sch, struct sd_out *out)
{
	struct data *data;
	if ((data = sd_malloc(sizeof(struct data))) == NULL)
	{
		sd_err("failed to alloc solver");
		return NULL;
	}
    data->cont = SD_CONT;
    data->out = out;
    data->sch = sch;
    data->sol = sd_sol_defaults;
    data->sol.data = data;
    return &data->sol;
}