char* bfam_dictionary_get_value(bfam_dictionary_t *d, const char *key) { if(!bfam_dictionary_contains(d,key)) return NULL; const size_t keylen = strlen(key); char *u = bfam_malloc(sizeof(char)*(keylen+2)); memcpy(u, key, keylen); u[keylen] = BFAM_KEYVALUE_SPLIT; u[keylen+1] = '\0'; char *value = NULL; void *arg[2]; arg[0] = u; arg[1] = &value; bfam_critbit0_allprefixed(&(d->t),u, &bfam_dictionary_get_value_handle,arg); bfam_free(u); return value; }
int bfam_dictionary_insert(bfam_dictionary_t *d, const char *key, const char *val) { const size_t keylen = strlen(key); const size_t vallen = strlen(val); char *keyval = bfam_malloc(sizeof(char)*(keylen+vallen+2)); memcpy(keyval, key, keylen); if(bfam_dictionary_contains(d,key)) { free(keyval); return 1; } keyval[keylen] = BFAM_KEYVALUE_SPLIT; memcpy(&keyval[keylen+1], val, vallen); keyval[keylen+vallen+1] = '\0'; int rval = bfam_critbit0_insert(&(d->t), keyval); if(rval == 2) ++d->num_entries; bfam_free(keyval); return rval; }
void bfam_cl_print_platforms_devices() { // get number of platforms cl_uint plat_count; BFAM_CL_SAFE_CALL(clGetPlatformIDs(0, NULL, &plat_count)); // allocate memory, get list of platforms cl_platform_id *platforms = (cl_platform_id *) bfam_malloc(plat_count*sizeof(cl_platform_id)); BFAM_CL_SAFE_CALL(clGetPlatformIDs(plat_count, platforms, NULL)); for(cl_uint i = 0; i < plat_count; ++i) { // get platform vendor name char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(buf), buf, NULL)); BFAM_INFO("platform %d: vendor '%s'", i, buf); // get number of devices in platform cl_uint dev_count; BFAM_CL_SAFE_CALL(clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &dev_count)); cl_device_id *devices = (cl_device_id *) bfam_malloc(dev_count*sizeof(cl_device_id)); // get list of devices in platform BFAM_CL_SAFE_CALL(clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, dev_count, devices, NULL)); // iterate over devices for(cl_uint j = 0; j < dev_count; ++j) { char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(buf), buf, NULL)); BFAM_INFO(" device %d: '%s'", j, buf); } bfam_free(devices); } bfam_free(platforms); }
void bfam_ts_adams_free(bfam_ts_adams_t *ts) { BFAM_LDEBUG("ADAMS FREE"); if (ts->lsrk != NULL) { bfam_ts_lsrk_free(ts->lsrk); bfam_free(ts->lsrk); } ts->lsrk = NULL; bfam_communicator_free(ts->comm); bfam_free(ts->comm); ts->comm = NULL; bfam_dictionary_clear(&ts->elems); /* bfam_free_aligned(ts->A); ts->A = NULL; */ ts->nStages = 0; ts->t = NAN; bfam_ts_free(&ts->base); }
int bfam_dictionary_contains(bfam_dictionary_t *d, const char *key) { const size_t keylen = strlen(key); char *u = bfam_malloc(sizeof(char)*(keylen+2)); memcpy(u, key, keylen); u[keylen] = BFAM_KEYVALUE_SPLIT; u[keylen+1] = '\0'; int found = 0; bfam_critbit0_allprefixed(&(d->t),u, &bfam_dictionary_contains_check,&found); bfam_free(u); return found; }
/* Read a line from stdin. C makes things simple. :) * From http://stackoverflow.com/a/314422/1148634 */ static char * read_a_line(void) { char * line = (char *) bfam_malloc(MAX_NAME_LEN), * linep = line; size_t lenmax = MAX_NAME_LEN, len = lenmax; int c; if(line == NULL) return NULL; for(;;) { c = fgetc(stdin); if(c == EOF) break; if(--len == 0) { char *linen = (char *) bfam_realloc(linep, lenmax *= 2); len = lenmax; if(linen == NULL) { bfam_free(linep); return NULL; } line = linen + (line - linep); linep = linen; } if((*line++ = c) == '\n') break; } *line = '\0'; return linep; }
void bfam_cl_create_context_on(const char *plat_name, const char *dev_name, cl_uint idx, cl_context *ctx, cl_command_queue *queue, int enable_profiling) { char dev_sel_buf[MAX_NAME_LEN]; char platform_sel_buf[MAX_NAME_LEN]; // get number of platforms cl_uint plat_count; BFAM_CL_SAFE_CALL(clGetPlatformIDs(0, NULL, &plat_count)); // allocate memory, get list of platform handles cl_platform_id *platforms = (cl_platform_id *) bfam_malloc(plat_count*sizeof(cl_platform_id)); BFAM_CL_SAFE_CALL(clGetPlatformIDs(plat_count, platforms, NULL)); // print menu, if requested #ifndef BFAM_CL_HELPER_FORCE_INTERACTIVE if (plat_name == CHOOSE_INTERACTIVELY) // yes, we want exactly that pointer #endif { puts("Choose platform:"); for (cl_uint i = 0; i < plat_count; ++i) { char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(buf), buf, NULL)); printf("[%d] %s\n", i, buf); } printf("Enter choice: "); fflush(stdout); char *sel = read_a_line(); if (!sel) { BFAM_ABORT("error reading line from stdin"); } int sel_int = BFAM_MIN(BFAM_MAX(0, atoi(sel)), (int) plat_count-1); bfam_free(sel); BFAM_CL_SAFE_CALL(clGetPlatformInfo(platforms[sel_int], CL_PLATFORM_VENDOR, sizeof(platform_sel_buf), platform_sel_buf, NULL)); plat_name = platform_sel_buf; } // iterate over platforms for (cl_uint i = 0; i < plat_count; ++i) { // get platform name char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(buf), buf, NULL)); // does it match? if (!plat_name || strstr(buf, plat_name)) { // get number of devices in platform cl_uint dev_count; BFAM_CL_SAFE_CALL(clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &dev_count)); // allocate memory, get list of device handles in platform cl_device_id *devices = (cl_device_id *) bfam_malloc(dev_count*sizeof(cl_device_id)); BFAM_CL_SAFE_CALL(clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, dev_count, devices, NULL)); // {{{ print device menu, if requested #ifndef BFAM_CL_HELPER_FORCE_INTERACTIVE if (dev_name == CHOOSE_INTERACTIVELY) // yes, we want exactly that pointer #endif { puts("Choose device:"); for (cl_uint j = 0; j < dev_count; ++j) { char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(buf), buf, NULL)); printf("[%d] %s\n", j, buf); } printf("Enter choice: "); fflush(stdout); char *sel = read_a_line(); if (!sel) { BFAM_ABORT("error reading line from stdin"); abort(); /* Add this for the clang static analysis */ } int int_sel = BFAM_MIN(BFAM_MAX(0, atoi(sel)), (int) dev_count-1); bfam_free(sel); BFAM_CL_SAFE_CALL(clGetDeviceInfo(devices[int_sel], CL_DEVICE_NAME, sizeof(dev_sel_buf), dev_sel_buf, NULL)); dev_name = dev_sel_buf; } // }}} // iterate over devices for (cl_uint j = 0; j < dev_count; ++j) { // get device name char buf[MAX_NAME_LEN]; BFAM_CL_SAFE_CALL(clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(buf), buf, NULL)); // does it match? if (!dev_name || strstr(buf, dev_name)) { if (idx == 0) { cl_platform_id plat = platforms[i]; cl_device_id dev = devices[j]; bfam_free(devices); bfam_free(platforms); // create a context cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0 }; cl_int status; *ctx = clCreateContext( cps, 1, &dev, NULL, NULL, &status); BFAM_CL_CHECK(status, "clCreateContext"); // create a command queue cl_command_queue_properties qprops = 0; if (enable_profiling) qprops |= CL_QUEUE_PROFILING_ENABLE; *queue = clCreateCommandQueue(*ctx, dev, qprops, &status); BFAM_CL_CHECK(status, "clCreateCommandQueue"); return; } else --idx; } } bfam_free(devices); } } bfam_free(platforms); BFAM_ABORT("create_context_on: specified device not found."); }
void bfam_ts_adams_step(bfam_ts_t *a_ts, bfam_long_real_t dt) { /* cast to the proper type */ bfam_ts_adams_t *ts = (bfam_ts_adams_t *)a_ts; bfam_ts_adams_allprefix_t data; data.ts = ts; data.dt = dt; /* * start the communication */ bfam_communicator_start(ts->comm); /* * do the intra work */ bfam_dictionary_allprefixed_ptr(&ts->elems, "", &bfam_ts_adams_intra_rhs, &data); /* * finish the communication */ bfam_communicator_finish(ts->comm); /* * do the inter work */ bfam_dictionary_allprefixed_ptr(&ts->elems, "", &bfam_ts_adams_inter_rhs, &data); if (ts->lsrk) { /* * If we are using RK, we want to tell the RK scheme to use the next rate * for storage (not the current rate which is valid */ ts->lsrk->t = ts->t; char rate_prefix[BFAM_BUFSIZ]; snprintf(rate_prefix, BFAM_BUFSIZ, "%s%d_", BFAM_ADAMS_PREFIX, (ts->currentStage + 1) % ts->nStages); BFAM_LDEBUG("Adams step: RK rate rate_prefix %s", rate_prefix); BFAM_ASSERT(ts->lsrk->step_extended); ts->lsrk->step_extended((bfam_ts_t *)ts->lsrk, dt, rate_prefix, "", ""); if (ts->numSteps + 2 >= ts->nStages) { bfam_ts_lsrk_free(ts->lsrk); bfam_free(ts->lsrk); ts->lsrk = NULL; } } else /* q_{n+1} := q_{n} + dt \sum_{k=0}^{m} a_{k} dq_{n-k} */ bfam_dictionary_allprefixed_ptr(&ts->elems, "", &bfam_ts_adams_update, &data); /* shift the stage counter */ ts->currentStage = (ts->currentStage + 1) % ts->nStages; ts->numSteps++; /* update the stage time */ ts->t += dt; }