void pgm_messages_init (void) { char *log_mask, *min_log_level; size_t len; errno_t err; if (pgm_atomic_exchange_and_add32 (&messages_ref_count, 1) > 0) return; pgm_mutex_init (&messages_mutex); err = pgm_dupenv_s (&log_mask, &len, "PGM_LOG_MASK"); if (!err && len > 0) { unsigned int value = 0; if (1 == pgm_sscanf_s (log_mask, "0x%4x", &value)) pgm_log_mask = value; pgm_free (log_mask); } err = pgm_dupenv_s (&min_log_level, &len, "PGM_MIN_LOG_LEVEL"); if (!err && len > 0) { switch (min_log_level[0]) { case 'D': pgm_min_log_level = PGM_LOG_LEVEL_DEBUG; break; case 'T': pgm_min_log_level = PGM_LOG_LEVEL_TRACE; break; case 'M': pgm_min_log_level = PGM_LOG_LEVEL_MINOR; break; case 'N': pgm_min_log_level = PGM_LOG_LEVEL_NORMAL; break; case 'W': pgm_min_log_level = PGM_LOG_LEVEL_WARNING; break; case 'E': pgm_min_log_level = PGM_LOG_LEVEL_ERROR; break; case 'F': pgm_min_log_level = PGM_LOG_LEVEL_FATAL; break; default: break; } pgm_free (min_log_level); } }
void gen_ball_border (void) { PGM *pgm[3]; int i, x, y; PGM *main; int next_ball = 0; #define next ({ next_ball = (next_ball+1) % 3; pgm[next_ball]; }) for (i=0; i < 3; i++) { unsigned int color = i+1; pgm[i] = pgm_alloc (); pgm_set_plane_count (pgm[i], 2); pgm_resize (pgm[i], 4, 4); pgm_draw_pixel (pgm[i], 1, 0, color); pgm_draw_pixel (pgm[i], 0, 1, color); pgm_draw_pixel (pgm[i], 1, 1, color); pgm_draw_pixel (pgm[i], 2, 1, color); pgm_draw_pixel (pgm[i], 1, 2, color); } main = pgm_alloc (); pgm_set_plane_count (main, 2); for (x=0; x < 128; x += 8) pgm_paste (main, next, x, 0); for (x=128-4; x >= 0; x -= 8) pgm_paste (main, next, x, 31-4); pgm_write (main, "images/ballborder.pgm"); pgm_write_xbmset (main, "images/ballborder.xbm", "ballborder"); pgm_free (main); }
static inline void _pgm_heap_free ( void* mem ) { # ifdef CONFIG_USE_HEAPALLOC HeapFree (GetProcessHeap(), 0, mem); # else pgm_free (mem); # endif }
int main(int argc, char* argv[]) { /* Arguments */ if ( argc != 2 ) { printf("\nUsage: %s file \n\n", argv[0]); exit(0); } pgm_t* src = pgm_read(argv[1]); pgm_t* dest = pgm_create_empty(src->cols, src->rows, src->maxval); for (int i = 1; i < dest->rows - 1; i++) { for (int j = 1; j < dest->cols - 1; j++) { gray g = PGM_AT(src, i, j); gray c1 = PGM_AT(src, i-1, j-1); gray c2 = PGM_AT(src, i-1, j+1); gray c3 = PGM_AT(src, i+1, j-1); gray c4 = PGM_AT(src, i+1, j+1); gray s1 = PGM_AT(src, i-1, j); gray s2 = PGM_AT(src, i, j-1); gray s3 = PGM_AT(src, i+1, j); gray s4 = PGM_AT(src, i, j+1); PGM_AT(dest, i, j) = ( c1 + c2 + c3 + c4 + 2 * s1 + 2 * s2 + 2 * s3 + 2 * s4 + 4 * g) / 16; } } pgm_write(dest, true); pgm_free(dest); pgm_free(src); return 0; }
/* Generate the multi-color border */ void gen_mborder (void) { PGM *pgm; XBMSET *xbmset; FILE *fp; pgm = pgm_alloc (); pgm_set_plane_count (pgm, 2); pgm_draw_border (pgm, 3, PGM_DARK(pgm)); pgm_draw_border (pgm, 2, PGM_BRIGHT(pgm)); pgm_draw_border (pgm, 1, PGM_WHITE(pgm)); pgm_write (pgm, "images/mborder.pgm"); pgm_write_xbmset (pgm, "images/mborder.xbm", "mborder"); pgm_free (pgm); }
int main(int argc, char* argv[]) { /* Arguments */ if ( argc != 2 ) { printf("\nUsage: %s <file>\n\n", argv[0]); exit(0); } pgm_t* src = pgm_read(argv[1]); dmap_t* sobx = sobel_x(src); dmap_t* soby = sobel_y(src); dmap_t* sobm = magnitude(src, sobx, soby); dmap_t* mxx = multiply(src, sobx, sobx); dmap_t* myy = multiply(src, soby, soby); dmap_t* mxy = multiply(src, sobx, soby); dmap_t* mxxf = binomial_filter(mxx); dmap_t* myyf = binomial_filter(myy); dmap_t* mxyf = binomial_filter(mxy); dmap_t* harris1 = harris(src, mxxf, myyf, mxyf, 1); dmap_t* harris1f = harris(src, mxxf, myyf, mxyf, 1); dmap_write(sobx, argv[1], "sobel_x"); dmap_write(soby, argv[1], "sobel_y"); dmap_write(sobm, argv[1], "sobel_magnitude"); dmap_write(mxx, argv[1], "xx"); dmap_write(myy, argv[1], "yy"); dmap_write(mxy, argv[1], "xy"); dmap_write(mxxf, argv[1], "xxf"); dmap_write(myyf, argv[1], "yyf"); dmap_write(mxyf, argv[1], "xyf"); dmap_write(harris1, argv[1], "harris1"); dmap_write(harris1f, argv[1], "harris1f"); pgm_free(src); dmap_free(sobx); dmap_free(soby); dmap_free(sobm); dmap_free(mxx); dmap_free(myy); dmap_free(mxy); dmap_free(harris1); return 0; }
void do_cross_validation(struct svm_problem *prob, struct svm_parameter *param,int nr_fold){ int i; int total_correct = 0; double total_error = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; double *target = Malloc(double,prob->l); svm_cross_validation(prob,param,nr_fold,target); if(param->svm_type == EPSILON_SVR || param->svm_type == NU_SVR) { for(i=0;i<prob->l;i++) { double y = prob->y[i]; double v = target[i]; total_error += (v-y)*(v-y); sumv += v; sumy += y; sumvv += v*v; sumyy += y*y; sumvy += v*y; } elog(INFO,"Cross Validation Mean squared error = %g\n",total_error/prob->l); elog(INFO,"Cross Validation Squared correlation coefficient = %g\n", ((prob->l*sumvy-sumv*sumy)*(prob->l*sumvy-sumv*sumy))/ ((prob->l*sumvv-sumv*sumv)*(prob->l*sumyy-sumy*sumy)) ); } else { for(i=0;i<prob->l;i++) if(target[i] == prob->y[i]) ++total_correct; elog(INFO,"Cross Validation Accuracy = %g%%\n",100.0*total_correct/prob->l); } pgm_free(target); }
PGM_GNUC_INTERNAL bool pgm_time_init ( pgm_error_t** error ) { char *pgm_timer; size_t envlen; errno_t err; if (pgm_atomic_exchange_and_add32 (&time_ref_count, 1) > 0) return TRUE; /* user preferred time stamp function */ err = pgm_dupenv_s (&pgm_timer, &envlen, "PGM_TIMER"); if (0 != err || 0 == envlen) { pgm_timer = pgm_strdup ( /* default time stamp function */ #if defined(_WIN32) "MMTIME" #else "GETTIMEOFDAY" #endif ); } pgm_time_since_epoch = pgm_time_conv; switch (pgm_timer[0]) { #ifdef HAVE_FTIME case 'F': pgm_minor (_("Using ftime() timer.")); pgm_time_update_now = pgm_ftime_update; break; #endif #ifdef HAVE_CLOCK_GETTIME case 'C': pgm_minor (_("Using clock_gettime() timer.")); pgm_time_update_now = pgm_clock_update; break; #endif #ifdef HAVE_DEV_RTC case 'R': pgm_minor (_("Using /dev/rtc timer.")); pgm_time_update_now = pgm_rtc_update; pgm_time_since_epoch = pgm_time_conv_from_reset; break; #endif #ifdef HAVE_RDTSC case 'T': pgm_minor (_("Using TSC timer.")); pgm_time_update_now = pgm_tsc_update; pgm_time_since_epoch = pgm_time_conv_from_reset; break; #endif #ifdef HAVE_DEV_HPET case 'H': pgm_minor (_("Using HPET timer.")); pgm_time_update_now = pgm_hpet_update; pgm_time_since_epoch = pgm_time_conv_from_reset; break; #endif #ifdef HAVE_GETTIMEOFDAY case 'G': pgm_minor (_("Using gettimeofday() timer.")); pgm_time_update_now = pgm_gettimeofday_update; break; #endif #ifdef _WIN32 case 'M': pgm_minor (_("Using multi-media timer.")); pgm_time_update_now = pgm_mmtime_update; pgm_time_since_epoch = pgm_time_conv_from_reset; break; case 'Q': pgm_minor (_("Using QueryPerformanceCounter() timer.")); pgm_time_update_now = pgm_queryperformancecounter_update; pgm_time_since_epoch = pgm_time_conv_from_reset; break; #endif default: pgm_set_error (error, PGM_ERROR_DOMAIN_TIME, PGM_ERROR_FAILED, _("Unsupported time stamp function: PGM_TIMER=%s"), pgm_timer); pgm_free (pgm_timer); goto err_cleanup; } /* clean environment copy */ pgm_free (pgm_timer); #ifdef HAVE_DEV_RTC if (pgm_time_update_now == pgm_rtc_update) { pgm_error_t* sub_error = NULL; if (!pgm_rtc_init (&sub_error)) { pgm_propagate_error (error, sub_error); goto err_cleanup; } } #endif #ifdef _WIN32 if (pgm_time_update_now == pgm_queryperformancecounter_update) { /* MSDN statement: The frequency cannot change while the system is running. * http://msdn.microsoft.com/en-us/library/ms644905(v=vs.85).aspx */ LARGE_INTEGER frequency; if (QueryPerformanceFrequency (&frequency)) { tsc_khz = (uint_fast32_t)(frequency.QuadPart / 1000LL); pgm_minor (_("High-resolution performance counter frequency %lld Hz"), frequency.QuadPart); } else { const DWORD save_errno = GetLastError(); char winstr[1024]; pgm_set_error (error, PGM_ERROR_DOMAIN_TIME, PGM_ERROR_FAILED, _("No supported high-resolution performance counter: %s"), pgm_win_strerror (winstr, sizeof (winstr), save_errno)); goto err_cleanup; } set_tsc_mul (tsc_khz); } #endif /* _WIN32 */ #ifdef HAVE_RDTSC if (pgm_time_update_now == pgm_tsc_update) { char *rdtsc_frequency; #ifdef HAVE_PROC_CPUINFO /* attempt to parse clock ticks from kernel */ FILE *fp = fopen ("/proc/cpuinfo", "r"); if (fp) { char buffer[1024]; while (!feof(fp) && fgets (buffer, sizeof(buffer), fp)) { if (strstr (buffer, "cpu MHz")) { const char *p = strchr (buffer, ':'); if (p) tsc_khz = atoi (p + 1) * 1000; break; } } fclose (fp); } #elif defined(_WIN32) /* core frequency HKLM/Hardware/Description/System/CentralProcessor/0/~Mhz */ HKEY hKey; if (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey)) { DWORD dwData = 0; DWORD dwDataSize = sizeof (dwData); if (ERROR_SUCCESS == RegQueryValueExA (hKey, "~MHz", NULL, NULL, (LPBYTE)&dwData, &dwDataSize)) { tsc_khz = dwData * 1000; pgm_minor (_("Registry reports central processor frequency %u MHz"), (unsigned)dwData); /* dump processor name for comparison aid of obtained frequency */ char szProcessorBrandString[48]; dwDataSize = sizeof (szProcessorBrandString); if (ERROR_SUCCESS == RegQueryValueExA (hKey, "ProcessorNameString", NULL, NULL, (LPBYTE)szProcessorBrandString, &dwDataSize)) { pgm_minor (_("Processor Brand String \"%s\""), szProcessorBrandString); } } else { const DWORD save_errno = GetLastError(); char winstr[1024]; pgm_warn (_("Registry query on HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz failed: %s"), pgm_win_strerror (winstr, sizeof (winstr), save_errno)); } RegCloseKey (hKey); } #elif defined(__APPLE__) /* nb: RDTSC is non-functional on Darwin */ uint64_t cpufrequency; size_t len; len = sizeof (cpufrequency); if (0 == sysctlbyname ("hw.cpufrequency", &cpufrequency, &len, NULL, 0)) { tsc_khz = (uint_fast32_t)(cpufrequency / 1000); } #elif defined(__FreeBSD__) /* frequency in Mhz */ unsigned long clockrate; size_t len; len = sizeof (clockrate); if (0 == sysctlbyname ("hw.clockrate", &clockrate, &len, NULL, 0)) { tsc_khz = (uint_fast32_t)(clockrate * 1000); } #elif defined(KSTAT_DATA_INT32) /* ref: http://developers.sun.com/solaris/articles/kstatc.html */ kstat_ctl_t* kc; kstat_t* ksp; kstat_named_t* kdata; if (NULL != (kc = kstat_open()) && NULL != (ksp = kstat_lookup (kc, "cpu_info", -1, NULL)) && KSTAT_TYPE_NAMED == ksp->ks_type && -1 != kstat_read (kc, ksp, NULL) && NULL != (kdata = kstat_data_lookup (ksp, "clock_MHz")) && KSTAT_DATA_INT32 == kdata->data_type) { tsc_khz = (uint_fast32_t)(kdata->value.i32 * 1000); kstat_close (kc); } #endif /* !_WIN32 */ /* e.g. export RDTSC_FREQUENCY=3200.000000 * * Value can be used to override kernel tick rate as well as internal calibration */ err = pgm_dupenv_s (&rdtsc_frequency, &envlen, "RDTSC_FREQUENCY"); if (0 == err && envlen > 0) { tsc_khz = atoi (rdtsc_frequency) * 1000; pgm_free (rdtsc_frequency); } #ifndef _WIN32 /* calibrate */ if (0 >= tsc_khz) { pgm_error_t* sub_error = NULL; if (!pgm_tsc_init (&sub_error)) { pgm_propagate_error (error, sub_error); goto err_cleanup; } } #endif pgm_minor (_("TSC frequency set at %u KHz"), (unsigned)(tsc_khz)); set_tsc_mul (tsc_khz); } #endif /* HAVE_RDTSC */ #ifdef HAVE_DEV_HPET if (pgm_time_update_now == pgm_hpet_update) { pgm_error_t* sub_error = NULL; if (!pgm_hpet_init (&sub_error)) { pgm_propagate_error (error, sub_error); goto err_cleanup; } } #endif pgm_time_update_now(); /* calculate relative time offset */ #if defined(HAVE_DEV_RTC) || defined(HAVE_RDTSC) || defined(HAVE_DEV_HPET) || defined(_WIN32) if ( 0 # ifdef HAVE_DEV_RTC || pgm_time_update_now == pgm_rtc_update # endif # ifdef HAVE_RDTSC || pgm_time_update_now == pgm_tsc_update # endif # ifdef HAVE_DEV_HPET || pgm_time_update_now == pgm_hpet_update # endif # ifdef _WIN32 || pgm_time_update_now == pgm_mmtime_update || pgm_time_update_now == pgm_queryperformancecounter_update # endif ) { # if defined( HAVE_GETTIMEOFDAY ) rel_offset = pgm_gettimeofday_update() - pgm_time_update_now(); # elif defined( HAVE_FTIME ) rel_offset = pgm_ftime_update() - pgm_time_update_now(); # else # error "gettimeofday() or ftime() required to calculate counter offset" # endif } #else rel_offset = 0; #endif /* update Windows timer resolution to 1ms */ #ifdef _WIN32 TIMECAPS tc; if (TIMERR_NOERROR == timeGetDevCaps (&tc, sizeof (TIMECAPS))) { wTimerRes = min (max (tc.wPeriodMin, 1 /* ms */), tc.wPeriodMax); timeBeginPeriod (wTimerRes); pgm_debug ("Set timer resolution to %ums.", wTimerRes); } else { pgm_warn (_("Unable to determine timer device resolution.")); } #endif return TRUE; err_cleanup: pgm_atomic_dec32 (&time_ref_count); return FALSE; }
struct svm_model* charptr2svm_model( char* str){ int i,j; unsigned int n_nodes; int tem_prob = 0; char *ptr = str; struct svm_model *model = (struct svm_model*) pgm_malloc (sizeof(struct svm_model)); model->rho = NULL; model->probA = NULL; model->probB = NULL; model->sv_indices = NULL; model->label = NULL; model->nSV = NULL; if(sscanf(ptr,"#%d#%d#%d#%lf#%lf#%d#%d",&model->param.svm_type,&model->param.kernel_type,&model->param.degree,&model->param.gamma,&model->param.coef0,&model->nr_class,&model->l) < 7){ pgm_free(model); return NULL; } for(i = 0; i < 8; i++) ptr = next_delimeter(ptr); model->rho = (double*) pgm_malloc(sizeof(double)*(model->nr_class*(model->nr_class-1)/2)); for(i = 0; i < model->nr_class*(model->nr_class-1)/2;i++){ sscanf(ptr,"%lf",&model->rho[i]); ptr = next_delimeter(ptr); } model->label = (int*) pgm_malloc (sizeof(int)*model->nr_class); for(i=0;i<model->nr_class;i++){ sscanf(ptr,"%d",&model->label[i]); ptr = next_delimeter(ptr); } do{ sscanf(ptr,"%d",&tem_prob); ptr = next_delimeter(ptr); if(tem_prob == 1){ model->probA = (double*) pgm_malloc (sizeof(double)*(model->nr_class*(model->nr_class-1)/2)); for(i=0;i<(model->nr_class*(model->nr_class-1)/2);i++){ sscanf(ptr,"%lf",&model->probA[i]); ptr = next_delimeter(ptr); } }else if(tem_prob == 2){ model->probB = (double*) pgm_malloc (sizeof(double)*(model->nr_class*(model->nr_class-1)/2)); for(i=0;i<(model->nr_class*(model->nr_class-1)/2);i++){ sscanf(ptr,"%lf",&model->probB[i]); ptr = next_delimeter(ptr); } }else if(tem_prob == 3){ model->nSV = (int*) pgm_malloc(sizeof(int)*(model->nr_class*(model->nr_class-1)/2)); for(i=0;i<model->nr_class;i++){ sscanf(ptr,"%d",&model->nSV[i]); ptr = next_delimeter(ptr); } } }while(tem_prob); model->sv_coef = (double**) pgm_malloc (sizeof(double*)*model->nr_class - 1); for(i = 0; i < model->nr_class - 1; i++) model->sv_coef[i] = (double*) pgm_malloc (sizeof(double)*model->l); model->SV = (struct svm_node**) pgm_malloc (sizeof(struct svm_node)*model->l); for(i=0;i<model->l;i++){ for(j=0;j<model->nr_class-1;j++){ sscanf(ptr,"%lf",&(model->sv_coef[j][i])); ptr = next_delimeter(ptr); } sscanf(ptr,"%d",&n_nodes); ptr = next_delimeter(ptr); model->SV[i] = (struct svm_node*) pgm_malloc(sizeof(struct svm_node)*n_nodes); for(j = 0; j < n_nodes; j++){ sscanf(ptr,"%d#%lf#",&(model->SV[i][j].index),&(model->SV[i][j].value)); ptr = next_delimeter(ptr); ptr = next_delimeter(ptr); } } return model; }
void* thread(void* _graph_t) { // All within the same process, so no need to call pgm_find_graph(). char tabbuf[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; int iterations = 0; int ret = 0; const graph_t& graph = *((graph_t*)_graph_t); node_t node; CheckError(pgm_claim_any_node(graph, &node)); tabbuf[node.node] = '\0'; int out_degree = pgm_get_degree_out(node); int in_degree = pgm_get_degree_in(node); edge_t* out_edges = (edge_t*)calloc(out_degree, sizeof(edge_t)); edge_t* in_edges = (edge_t*)calloc(in_degree, sizeof(edge_t)); msg_t** out_bufs = 0; msg_t** in_bufs = 0; CheckError(pgm_get_edges_out(node, out_edges, out_degree)); if(out_degree) { out_bufs = (msg_t**)calloc(out_degree, sizeof(msg_t*)); for(int i = 0; i < out_degree; ++i) { out_bufs[i] = (msg_t*)pgm_get_edge_buf_p(out_edges[i]); } } CheckError(pgm_get_edges_in(node, in_edges, in_degree)); if(in_degree) { in_bufs = (msg_t**)calloc(in_degree, sizeof(msg_t*)); for(int i = 0; i < in_degree; ++i) { in_bufs[i] = (msg_t*)pgm_get_edge_buf_c(in_edges[i]); } } bool is_src = (in_degree == 0); // an extra buffer for the sake of testing msg_t* extra_buf = 0; if(is_src) { extra_buf = (msg_t*)pgm_malloc_edge_buf_p(out_edges[0]); CheckError((extra_buf) ? 0 : -1); } pthread_barrier_wait(&init_barrier); if(!errors) { do { if(!is_src) { ret = pgm_wait(node); } else { if(iterations > TOTAL_ITERATIONS) ret = PGM_TERMINATE; } if(ret != PGM_TERMINATE) { CheckError(ret); if(is_src) { // (for testing) swap buffers extra_buf = (msg_t*)pgm_swap_edge_buf_p(out_edges[0], extra_buf); #ifdef TEST_INCREMENTAL_PRODUCTION uint16_t halfval = (iterations % 2 != 0) ? 0xdead : 0xbeef; for(int i = 0; i < out_degree; ++i) if(out_bufs[i]) *((uint16_t*)out_bufs[i]) = halfval; #else uint32_t val = 0xdeadbeef; for(int i = 0; i < out_degree; ++i) if(out_bufs[i]) out_bufs[i]->val = val; #endif usleep(1*1000); } else { uint32_t val = 0; for(int i = 0; i < in_degree; ++i) if(in_bufs[i]) val += in_bufs[i]->val; if(in_degree && out_degree) { // (for testing) swap the consumer and producer buffers CheckError(pgm_swap_edge_bufs(in_bufs[0], out_bufs[0])); msg_t* tmp = in_bufs[0]; in_bufs[0] = out_bufs[0]; out_bufs[0] = tmp; } for(int i = 0; i < out_degree; ++i) if(out_bufs[i]) out_bufs[i]->val = val; } CheckError(pgm_complete(node)); iterations++; } else { fprintf(stdout, "%s%d terminates\n", tabbuf, node.node); if(is_src) CheckError(pgm_terminate(node)); } } while(ret != PGM_TERMINATE); } pthread_barrier_wait(&init_barrier); if(extra_buf) pgm_free(extra_buf); CheckError(pgm_release_node(node)); free(in_edges); free(out_edges); free(out_bufs); free(in_bufs); pthread_exit(0); }
/* resets the last errstr */ FANN_EXTERNAL void FANN_API fann_reset_errstr(struct fann_error *errdat) { if(errdat->errstr != NULL) pgm_free(errdat->errstr); errdat->errstr = NULL; }