array_array_number_t matrix_fill(card_t rows, card_t cols, number_t value) { #ifdef DPS return TOP_LEVEL_linalg_matrixFill_dps(storage_alloc(MATRIX_ROWS_OFFSET(rows, cols, rows)), rows, cols, value, rows, cols, 0); #else return TOP_LEVEL_linalg_matrixFill(rows, cols, value); #endif }
void storage_realloc(storage_t *st, size_t size, at *init) { if (size < st->size) RAISEF("storage size cannot be reduced", st->backptr); size_t s = size*storage_sizeof[st->type]; size_t olds = st->size*storage_sizeof[st->type]; gptr olddata = st->data; if (st->kind == STS_NULL) { /* empty storage */ assert(st->data == NULL); storage_alloc(st, size, init); return; } else { /* reallocate memory and update srg */ if (st->kind == STS_MANAGED) MM_ANCHOR(olddata); if (st->type==ST_AT || st->type==ST_MPTR) st->data = mm_allocv(mt_refs, s); else st->data = mm_blob(s); if (st->data) { memcpy(st->data, olddata, olds); st->kind = STS_MANAGED; } } if (st->data == NULL) { st->data = olddata; RAISEF("not enough memory", NIL); } size_t oldsize = st->size; st->size = size; if (init) { /* temporarily clear read only flag to allow initialization */ bool isreadonly = st->isreadonly; storage_clear(st, init, oldsize); st->isreadonly = isreadonly; } }
void storage_load(storage_t *st, FILE *f) { if (st->type == ST_AT) RAISEF("cannot load an AT storage", NIL); if (st->data == NULL) { assert(st->size == 0); #if HAVE_FSEEKO off_t here = ftello(f); errno = 0; if (fseeko(f,0,SEEK_END)==-1) test_file_error(NULL, errno); off_t len = ftello(f); errno = 0; if (fseeko(f,here,SEEK_SET)==-1) test_file_error(NULL, errno); #else off_t here = ftell(f); errno = 0; if (fseek(f,0,SEEK_END)==-1) test_file_error(NULL, errno); int len = ftell(f); errno = 0; if (fseek(f,here,SEEK_SET)==-1) test_file_error(NULL, errno); #endif if (len==0) return; else storage_alloc(st,(size_t)len/storage_sizeof[st->type],0); } get_write_permit(st); char *pt = st->data; errno = 0; int nrec = fread(pt, storage_sizeof[st->type], st->size, f); if (nrec < st->size) RAISEF("file is too small",NIL); test_file_error(f, errno); }
int main(int argc, char** argv) { if(argc != 2) { printf("You should use the following format for running this program: %s <Number of Iterations>\n", argv[0]); exit(1); } int N = atoi(argv[1]); int rng = 42; srand(rng); array_number_t vec1 = vector_fill(DIM, 0.0); array_number_t vec2 = vector_fill(DIM, 0.0); array_number_t vec3 = vector_fill(DIM, 0.0); for(int i=0; i<DIM; i++) { vec1->arr[i] = dist(rng); vec2->arr[i] = dist(rng); vec3->arr[i] = dist(rng); } #ifdef HOIST storage_t s = storage_alloc(VECTOR_ALL_BYTES(DIM)); #endif timer_t t = tic(); double total = 0; for (int count = 0; count < N; ++count) { vec1->arr[0] += 1.0 / (2.0 + vec1->arr[0]); vec2->arr[10] += 1.0 / (2.0 + vec2->arr[10]); #ifdef DPS #ifndef HOIST storage_t s = storage_alloc(VECTOR_ALL_BYTES(DIM)); #endif #endif #ifdef ADD3 #ifdef DPS total += vectorSum(TOP_LEVEL_linalg_vectorAdd3_dps(s, vec1, vec2, vec3, DIM, DIM, DIM)); #else total += vectorSum(TOP_LEVEL_linalg_vectorAdd3(vec1, vec2, vec3)); #endif #elif DOT #ifdef DPS total += TOP_LEVEL_linalg_dot_prod_dps(s, vec1, vec2, DIM, DIM); #else total += TOP_LEVEL_linalg_dot_prod(vec1, vec2); #endif #elif CROSS #ifdef DPS total += vectorSum(TOP_LEVEL_linalg_cross_dps(s, vec1, vec2, DIM, DIM)); #else total += vectorSum(TOP_LEVEL_linalg_cross(vec1, vec2)); #endif #endif #ifdef DPS #ifndef HOIST storage_free(s, VECTOR_ALL_BYTES(DIM)); #endif #endif } float elapsed = toc2(t); printf("total =%f, time per call = %f ms\n", total, elapsed / (double)(N)); return 0; }
storage_t *new_storage_managed(storage_type_t t, size_t n, at *init) { storage_t *st = new_storage(t); if (n) storage_alloc(st, n, init); return st; }
void rfcm_alloc(io_handle_t *handle, io_buf_t *buf) { buf->base = storage_alloc(&storage); buf->len = (buf->base)? 512 : 0; }