static void test_timedwait(void) { clock_ticks_t before, after; int r; thr_event_clear(&event); printf("With event clear...\n"); before = clock_now(); printf(" current time: %" CLOCK_PRI_TICKS "\n", before); r = thr_event_wait_timeout(&event, 500); assert(r); after = clock_now(); printf(" after 500 ms wait: %" CLOCK_PRI_TICKS "\n", after); assert((after >= before + 450) && (after <= before + 550)); thr_event_raise(&event); printf("With event set...\n"); before = clock_now(); printf(" current time: %" CLOCK_PRI_TICKS "\n", before); r = thr_event_wait_timeout(&event, 500); assert(!r); after = clock_now(); printf(" after 500 ms wait: %" CLOCK_PRI_TICKS "\n", after); assert(after <= before + 50); }
void jl_gc_collect(void) { allocd_bytes = 0; if (is_gc_enabled) { JL_SIGATOMIC_BEGIN(); #ifdef GCTIME double t0 = clock_now(); #endif gc_mark(); #ifdef GCTIME ios_printf(ios_stderr, "mark time %.3f ms\n", (clock_now()-t0)*1000); #endif #if defined(MEMPROFILE) all_pool_stats(); big_obj_stats(); #endif #ifdef GCTIME t0 = clock_now(); #endif sweep_weak_refs(); gc_sweep(); #ifdef GCTIME ios_printf(ios_stderr, "sweep time %.3f ms\n", (clock_now()-t0)*1000); #endif run_finalizers(); JL_SIGATOMIC_END(); #ifdef OBJPROFILE print_obj_profile(); htable_reset(&obj_counts, 0); #endif } }
void jl_gc_collect(void) { size_t actual_allocd = allocd_bytes; total_allocd_bytes += allocd_bytes; allocd_bytes = 0; if (is_gc_enabled) { JL_SIGATOMIC_BEGIN(); jl_in_gc = 1; #if defined(GCTIME) || defined(GC_FINAL_STATS) double t0 = clock_now(); #endif gc_mark(); #ifdef GCTIME JL_PRINTF(JL_STDERR, "mark time %.3f ms\n", (clock_now()-t0)*1000); #endif #if defined(MEMPROFILE) all_pool_stats(); big_obj_stats(); #endif #ifdef GCTIME t0 = clock_now(); #endif sweep_weak_refs(); gc_sweep(); #ifdef GCTIME JL_PRINTF(JL_STDERR, "sweep time %.3f ms\n", (clock_now()-t0)*1000); #endif int nfinal = to_finalize.len; run_finalizers(); jl_in_gc = 0; JL_SIGATOMIC_END(); #if defined(GC_FINAL_STATS) total_gc_time += (clock_now()-t0); total_freed_bytes += freed_bytes; #endif #ifdef OBJPROFILE print_obj_profile(); htable_reset(&obj_counts, 0); #endif // tune collect interval based on current live ratio #if defined(MEMPROFILE) jl_printf(JL_STDERR, "allocd %ld, freed %ld, interval %ld, ratio %.2f\n", actual_allocd, freed_bytes, collect_interval, (double)freed_bytes/(double)actual_allocd); #endif if (freed_bytes < (7*(actual_allocd/10))) { if (collect_interval <= 2*(max_collect_interval/5)) collect_interval = 5*(collect_interval/2); } else { collect_interval = default_collect_interval; } freed_bytes = 0; // if a lot of objects were finalized, re-run GC to finish freeing // their storage if possible. if (nfinal > 100000) jl_gc_collect(); } }
int main(void) { clock_ticks_t before, after; before = clock_now(); printf("Current time: %" CLOCK_PRI_TICKS "\n", before); clock_wait(500); after = clock_now(); printf("After 500 ms: %" CLOCK_PRI_TICKS "\n", after); assert((after >= before + 450) && (after <= before + 550)); return 0; }
void jl_gc_init(void) { int szc[N_POOLS] = { 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, //#=18 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 288, 320, 352, 384, 416, 448, 480, 512, 640, 768, 896, 1024, 1536, 2048 }; int i; for(i=0; i < N_POOLS; i++) { norm_pools[i].osize = szc[i]; norm_pools[i].pages = NULL; norm_pools[i].freelist = NULL; ephe_pools[i].osize = szc[i]; ephe_pools[i].pages = NULL; ephe_pools[i].freelist = NULL; } htable_new(&finalizer_table, 0); arraylist_new(&to_finalize, 0); arraylist_new(&preserved_values, 0); arraylist_new(&weak_refs, 0); #ifdef OBJPROFILE htable_new(&obj_counts, 0); #endif #ifdef GC_FINAL_STATS process_t0 = clock_now(); #endif }
void jl_gc_collect(void) { allocd_bytes = 0; if (is_gc_enabled) { freed_bytes = 0; JL_SIGATOMIC_BEGIN(); #if defined(GCTIME) || defined(GC_FINAL_STATS) double t0 = clock_now(); #endif gc_mark(); #ifdef GCTIME JL_PRINTF(JL_STDERR, "mark time %.3f ms\n", (clock_now()-t0)*1000); #endif #if defined(MEMPROFILE) all_pool_stats(); big_obj_stats(); #endif #ifdef GCTIME t0 = clock_now(); #endif sweep_weak_refs(); gc_sweep(); #ifdef GCTIME JL_PRINTF(JL_STDERR, "sweep time %.3f ms\n", (clock_now()-t0)*1000); #endif run_finalizers(); JL_SIGATOMIC_END(); #if defined(GC_FINAL_STATS) total_gc_time += (clock_now()-t0); total_freed_bytes += freed_bytes; #endif #ifdef OBJPROFILE print_obj_profile(); htable_reset(&obj_counts, 0); #endif // tune collect interval based on current live ratio if (freed_bytes < ((2*collect_interval)/5)) { if (collect_interval <= (2*max_collect_interval)/5) collect_interval = (5*collect_interval)/2; } else { collect_interval = default_collect_interval; } } }
void jl_print_gc_stats(JL_STREAM *s) { double gct = total_gc_time/1e9; malloc_stats(); double ptime = clock_now()-process_t0; jl_printf(s, "exec time\t%.5f sec\n", ptime); jl_printf(s, "gc time \t%.5f sec (%2.1f%%)\n", gct, (gct/ptime)*100); struct mallinfo mi = mallinfo(); jl_printf(s, "malloc size\t%d MB\n", mi.uordblks/1024/1024); jl_printf(s, "total freed\t%llu b\n", total_freed_bytes); jl_printf(s, "free rate\t%.1f MB/sec\n", (total_freed_bytes/gct)/1024/1024); }
DLLEXPORT void jl_enq_send_req(ios_t *dest, ios_t *buf, int now) { pthread_mutex_lock(&q_mut); sendreq_t *req = ioq; sendreq_t **pr = &ioq; while (req != NULL) { if (req->fd == dest->fd) { if (now && !req->now) { // increase priority *pr = req->next; req->next = ioq; ioq = req; req->now = 1; } pthread_mutex_unlock(&q_mut); return; } pr = &req->next; req = req->next; } if (ioq_freelist != NULL) { req = ioq_freelist; ioq_freelist = ioq_freelist->next; } else { req = (sendreq_t*)malloc(sizeof(sendreq_t)); } req->fd = dest->fd; req->buf = buf; req->now = now; req->next = NULL; buf->userdata = (int64_t)(clock_now()*1e6); if (ioq == NULL) { ioq = req; } else { if (now) { req->next = ioq; ioq = req; } else { sendreq_t *r = ioq; while (r->next != NULL) { r = r->next; } r->next = req; } } pthread_mutex_unlock(&q_mut); pthread_cond_signal(&wake_cond); }
void print_gc_stats(void) { malloc_stats(); double ptime = clock_now()-process_t0; ios_printf(ios_stderr, "exec time\t%.5f sec\n", ptime); ios_printf(ios_stdout, "gc time \t%.5f sec (%2.1f%%)\n", total_gc_time, (total_gc_time/ptime)*100); struct mallinfo mi = mallinfo(); ios_printf(ios_stdout, "malloc size\t%d MB\n", mi.uordblks/1024/1024); ios_printf(ios_stdout, "total freed\t%llu b\n", total_freed_bytes); ios_printf(ios_stdout, "free rate\t%.1f MB/sec\n", (total_freed_bytes/total_gc_time)/1024/1024); }
static void uptime_init(void) { if(!boot_time) { FILE *fp; char *path; double up; fp = xfopenf(&path, "r", "%s/uptime", proc); if(fscanf(fp, "%lg %lg", &up, &idle_time) != 2) fatal(errno, "reading %s", path); fclose(fp); free(path); boot_time = clock_now() - up; } }
static void *run_io_thr(void *arg) { sigset_t set; sigemptyset(&set); sigaddset(&set, SIGFPE); sigaddset(&set, SIGINT); sigaddset(&set, SIGSEGV); pthread_sigmask(SIG_BLOCK, &set, NULL); while (1) { while (ioq == NULL) { pthread_mutex_lock(&wake_mut); pthread_cond_wait(&wake_cond, &wake_mut); pthread_mutex_unlock(&wake_mut); } assert(ioq != NULL); pthread_mutex_lock(&q_mut); sendreq_t *r = ioq; ioq = ioq->next; pthread_mutex_unlock(&q_mut); if (!r->now) { int64_t now = (int64_t)(clock_now()*1e6); int64_t waittime = r->buf->userdata+200-now; // microseconds if (waittime > 0) { struct timespec wt; wt.tv_sec = 0; wt.tv_nsec = waittime * 1000; nanosleep(&wt, NULL); } } pthread_mutex_lock(&r->buf->mutex); size_t sz; size_t n = r->buf->size; char *buf = ios_takebuf(r->buf, &sz); pthread_mutex_unlock(&r->buf->mutex); size_t nw; _os_write_all(r->fd, buf, n, &nw); julia_free(buf); pthread_mutex_lock(&q_mut); r->next = ioq_freelist; ioq_freelist = r; pthread_mutex_unlock(&q_mut); } return NULL; }
void jl_gc_init(void) { int szc[N_POOLS] = { 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, //#=18 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 288, 320, 352, 384, 416, 448, 480, 512, 640, 768, 896, 1024, 1536, 2048 }; int i; for(i=0; i < N_POOLS; i++) { norm_pools[i].osize = szc[i]; norm_pools[i].pages = NULL; norm_pools[i].freelist = NULL; ephe_pools[i].osize = szc[i]; ephe_pools[i].pages = NULL; ephe_pools[i].freelist = NULL; } htable_new(&finalizer_table, 0); arraylist_new(&to_finalize, 0); arraylist_new(&preserved_values, 0); arraylist_new(&weak_refs, 0); #ifdef OBJPROFILE htable_new(&obj_counts, 0); #endif #ifdef GC_FINAL_STATS process_t0 = clock_now(); #endif #ifdef _P64 // on a big memory machine, set max_collect_interval to totalmem/ncores/2 size_t maxmem = (uv_get_total_memory()/jl_cpu_cores())/2; if (maxmem > max_collect_interval) max_collect_interval = maxmem; #endif }
static value_t fl_time_now(value_t *args, u_int32_t nargs) { argcount("time.now", nargs, 0); (void)args; return mk_double(clock_now()); }
static ERL_NIF_TERM get(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM atom; ErlNifBinary kbin; struct cache *c; struct cache_node *n; struct cache_incr_node *in; struct timespec now; int incrqs, hashv, bkt; ERL_NIF_TERM ret; ErlNifTid tid; if (!enif_is_atom(env, argv[0])) return enif_make_badarg(env); atom = argv[0]; if (!enif_inspect_binary(env, argv[1], &kbin)) return enif_make_badarg(env); if ((c = get_cache(atom))) { enif_rwlock_rlock(c->lookup_lock); HASH_FIND(hh, c->lookup, kbin.data, kbin.size, n); if (!n) { enif_rwlock_runlock(c->lookup_lock); __sync_add_and_fetch(&c->miss, 1); enif_consume_timeslice(env, 10); return enif_make_atom(env, "notfound"); } if (n->expiry.tv_sec != 0) { clock_now(&now); if (n->expiry.tv_sec < now.tv_sec) { enif_rwlock_runlock(c->lookup_lock); __sync_add_and_fetch(&c->miss, 1); enif_consume_timeslice(env, 10); return enif_make_atom(env, "notfound"); } } in = enif_alloc(sizeof(*in)); memset(in, 0, sizeof(*in)); in->node = n; __sync_add_and_fetch(&c->hit, 1); tid = enif_thread_self(); HASH_SFH(&tid, sizeof(ErlNifTid), N_INCR_BKT, hashv, bkt); enif_mutex_lock(c->incr_lock[bkt]); TAILQ_INSERT_TAIL(&(c->incr_head[bkt]), in, entry); enif_mutex_unlock(c->incr_lock[bkt]); incrqs = __sync_add_and_fetch(&(c->incr_count), 1); ret = enif_make_resource_binary(env, n->val, n->val, n->vsize); enif_rwlock_runlock(c->lookup_lock); if (incrqs > 1024) enif_cond_broadcast(c->check_cond); enif_consume_timeslice(env, 20); return ret; } return enif_make_atom(env, "notfound"); }
static ERL_NIF_TERM put(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM atom; ErlNifBinary kbin, vbin; struct cache *c; struct cache_node *n, *ng; ErlNifUInt64 lifetime = 0; if (!enif_is_atom(env, argv[0])) return enif_make_badarg(env); atom = argv[0]; if (!enif_inspect_binary(env, argv[1], &kbin)) return enif_make_badarg(env); if (!enif_inspect_binary(env, argv[2], &vbin)) return enif_make_badarg(env); if ((c = get_cache(atom))) { enif_consume_timeslice(env, 1); } else { /* if we've been asked to put() in to a cache that doesn't exist yet then we should create it! */ ErlNifUInt64 max_size, min_q1_size; if (!enif_get_uint64(env, argv[3], &max_size)) return enif_make_badarg(env); if (!enif_get_uint64(env, argv[4], &min_q1_size)) return enif_make_badarg(env); c = new_cache(atom, max_size, min_q1_size); enif_consume_timeslice(env, 20); } if (argc > 5) if (!enif_get_uint64(env, argv[5], &lifetime)) return enif_make_badarg(env); n = enif_alloc(sizeof(*n)); memset(n, 0, sizeof(*n)); n->c = c; n->vsize = vbin.size; n->ksize = kbin.size; n->size = vbin.size + kbin.size; n->key = enif_alloc(kbin.size); memcpy(n->key, kbin.data, kbin.size); n->val = enif_alloc_resource(value_type, vbin.size); memcpy(n->val, vbin.data, vbin.size); n->q = &(c->q1); if (lifetime) { clock_now(&(n->expiry)); n->expiry.tv_sec += lifetime; } enif_rwlock_rwlock(c->cache_lock); enif_rwlock_rwlock(c->lookup_lock); HASH_FIND(hh, c->lookup, kbin.data, kbin.size, ng); if (ng) { enif_mutex_lock(c->ctrl_lock); destroy_cache_node(ng); enif_mutex_unlock(c->ctrl_lock); } TAILQ_INSERT_HEAD(&(c->q1.head), n, entry); c->q1.size += n->size; HASH_ADD_KEYPTR(hh, c->lookup, n->key, n->ksize, n); if (lifetime) { struct cache_node *rn; rn = RB_INSERT(expiry_tree, &(c->expiry_head), n); /* it's possible to get two timestamps that are the same, if this happens just bump us forwards by 1 usec until we're unique */ while (rn != NULL) { ++(n->expiry.tv_nsec); rn = RB_INSERT(expiry_tree, &(c->expiry_head), n); } } enif_rwlock_rwunlock(c->lookup_lock); enif_rwlock_rwunlock(c->cache_lock); enif_cond_broadcast(c->check_cond); enif_consume_timeslice(env, 50); return enif_make_atom(env, "ok"); }
static void * cache_bg_thread(void *arg) { struct cache *c = (struct cache *)arg; int i, dud; while (1) { enif_mutex_lock(c->ctrl_lock); /* if we've been told to die, quit this loop and start cleaning up */ if (c->flags & FL_DYING) { enif_mutex_unlock(c->ctrl_lock); break; } /* sleep until there is work to do */ enif_cond_wait(c->check_cond, c->ctrl_lock); __sync_add_and_fetch(&(c->wakeups), 1); dud = 1; /* we have to let go of ctrl_lock so we can take cache_lock then ctrl_lock again to get them back in the right order */ enif_mutex_unlock(c->ctrl_lock); enif_rwlock_rwlock(c->cache_lock); enif_mutex_lock(c->ctrl_lock); /* first process the promotion queue before we do any evicting */ for (i = 0; i < N_INCR_BKT; ++i) { enif_mutex_lock(c->incr_lock[i]); while (!TAILQ_EMPTY(&(c->incr_head[i]))) { struct cache_incr_node *n; n = TAILQ_FIRST(&(c->incr_head[i])); TAILQ_REMOVE(&(c->incr_head[i]), n, entry); __sync_sub_and_fetch(&(c->incr_count), 1); dud = 0; /* let go of the ctrl_lock here, we don't need it when we aren't looking at the incr_queue, and this way other threads can use it while we shuffle queue nodes around */ enif_mutex_unlock(c->incr_lock[i]); enif_mutex_unlock(c->ctrl_lock); if (n->node->q == &(c->q1)) { TAILQ_REMOVE(&(c->q1.head), n->node, entry); c->q1.size -= n->node->size; TAILQ_INSERT_HEAD(&(c->q2.head), n->node, entry); n->node->q = &(c->q2); c->q2.size += n->node->size; } else if (n->node->q == &(c->q2)) { TAILQ_REMOVE(&(c->q2.head), n->node, entry); TAILQ_INSERT_HEAD(&(c->q2.head), n->node, entry); } enif_free(n); /* take the ctrl_lock back again for the next loop around */ enif_mutex_lock(c->ctrl_lock); enif_mutex_lock(c->incr_lock[i]); } enif_mutex_unlock(c->incr_lock[i]); } /* let go of the ctrl_lock here for two reasons: 1. avoid lock inversion, because if we have evictions to do we will need to take lookup_lock, and we must take lookup_lock before taking ctrl_lock 2. if we don't need to do evictions, we're done with the structures that are behind ctrl_lock so we should give it up for others */ enif_mutex_unlock(c->ctrl_lock); /* do timed evictions -- if anything has expired, nuke it */ { struct cache_node *n; if ((n = RB_MIN(expiry_tree, &(c->expiry_head)))) { struct timespec now; clock_now(&now); while (n && n->expiry.tv_sec < now.tv_sec) { enif_mutex_lock(c->ctrl_lock); dud = 0; destroy_cache_node(n); enif_mutex_unlock(c->ctrl_lock); n = RB_MIN(expiry_tree, &(c->expiry_head)); } } } /* now check if we need to do ordinary size limit evictions */ if (c->q1.size + c->q2.size > c->max_size) { enif_rwlock_rwlock(c->lookup_lock); enif_mutex_lock(c->ctrl_lock); while ((c->q1.size + c->q2.size > c->max_size) && (c->q1.size > c->min_q1_size)) { struct cache_node *n; n = TAILQ_LAST(&(c->q1.head), cache_q); destroy_cache_node(n); } while (c->q1.size + c->q2.size > c->max_size) { struct cache_node *n; n = TAILQ_LAST(&(c->q2.head), cache_q); destroy_cache_node(n); } dud = 0; enif_mutex_unlock(c->ctrl_lock); enif_rwlock_rwunlock(c->lookup_lock); } if (dud) __sync_add_and_fetch(&(c->dud_wakeups), 1); /* now let go of the cache_lock that we took right back at the start of this iteration */ enif_rwlock_rwunlock(c->cache_lock); } /* first remove us from the atom_tree, so we get no new operations coming in */ enif_rwlock_rwlock(gbl->atom_lock); RB_REMOVE(atom_tree, &(gbl->atom_head), c->atom_node); enif_rwlock_rwunlock(gbl->atom_lock); enif_free(c->atom_node); /* now take all of our locks, to make sure any pending operations are done */ enif_rwlock_rwlock(c->cache_lock); enif_rwlock_rwlock(c->lookup_lock); enif_mutex_lock(c->ctrl_lock); c->atom_node = NULL; /* free the actual cache queues */ { struct cache_node *n, *nextn; nextn = TAILQ_FIRST(&(c->q1.head)); while ((n = nextn)) { nextn = TAILQ_NEXT(n, entry); destroy_cache_node(n); } nextn = TAILQ_FIRST(&(c->q2.head)); while ((n = nextn)) { nextn = TAILQ_NEXT(n, entry); destroy_cache_node(n); } } for (i = 0; i < N_INCR_BKT; ++i) enif_mutex_lock(c->incr_lock[i]); /* free the incr_queue */ for (i = 0; i < N_INCR_BKT; ++i) { struct cache_incr_node *in, *nextin; nextin = TAILQ_FIRST(&(c->incr_head[i])); while ((in = nextin)) { nextin = TAILQ_NEXT(in, entry); TAILQ_REMOVE(&(c->incr_head[i]), in, entry); in->node = 0; enif_free(in); } enif_mutex_unlock(c->incr_lock[i]); enif_mutex_destroy(c->incr_lock[i]); } /* unlock and destroy! */ enif_cond_destroy(c->check_cond); enif_mutex_unlock(c->ctrl_lock); enif_mutex_destroy(c->ctrl_lock); enif_rwlock_rwunlock(c->lookup_lock); enif_rwlock_destroy(c->lookup_lock); enif_rwlock_rwunlock(c->cache_lock); enif_rwlock_destroy(c->cache_lock); enif_free(c); return 0; }
int main() { // Initialize RNG dsfmt_gv_init_gen_rand(0); double t, tmin; // fib(20) assert(fib(20) == 6765); int f = 0; tmin = INFINITY; volatile int fibarg = 20; // prevent constant propagation for (int i=0; i<NITER; ++i) { t = clock_now(); for (int j = 0; j < 1000; j++) f += fib(fibarg); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("fib", tmin / 1000); // parse_bin tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); char s[11]; for (int k=0; k<1000 * 100; ++k) { uint32_t n = dsfmt_gv_genrand_uint32(); sprintf(s, "%x", n); uint32_t m = (uint32_t)parse_int(s, 16); assert(m == n); } t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("parse_int", tmin / 100); // // array constructor // tmin = INFINITY; // for (int i=0; i<NITER; ++i) { // t = clock_now(); // double *a = ones(200,200); // free(a); // t = clock_now()-t; // if (t < tmin) tmin = t; // } // print_perf("ones", tmin); // // // A*A' // //SUBROUTINE DGEMM(TRANSA,TRANSB,M,N,K,ALPHA,A,LDA,B,LDB,BETA,C,LDC) // double *b = ones(200, 200); // tmin = INFINITY; // for (int i=0; i<NITER; ++i) { // t = clock_now(); // double *c = matmul_aat(200, b); // free(c); // t = clock_now()-t; // if (t < tmin) tmin = t; // } // free(b); // print_perf("AtA", tmin); // mandel /* The initialization on the next line is deliberately volatile to * prevent gcc from optimizing away the entire loop. * (First observed in gcc 4.9.2) */ static volatile int mandel_sum_init = 0; int mandel_sum2 = mandel_sum_init; tmin = INFINITY; for (int i=0; i<NITER; ++i) { int *M; t = clock_now(); for (int j = 0; j < 100; j++) { M = mandelperf(); if (j == 0) { int mandel_sum = 0; // for (int ii = 0; ii < 21; ii++) { // for (int jj = 0; jj < 26; jj++) { // printf("%4d", M[26*ii + jj]); // } // printf("\n"); // } for (int k = 0; k < 21*26; k++) { mandel_sum += M[k]; } assert(mandel_sum == 14791); mandel_sum2 += mandel_sum; } free(M); } t = clock_now()-t; if (t < tmin) tmin = t; } assert(mandel_sum2 == 14791 * NITER); print_perf("mandel", tmin / 100); // sort tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); double *d = myrand(5000); quicksort(d, 0, 5000-1); free(d); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("quicksort", tmin); // pi sum double pi; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); pi = pisum(); t = clock_now()-t; if (t < tmin) tmin = t; } assert(fabs(pi-1.644834071848065) < 1e-12); print_perf("pi_sum", tmin); // rand mat stat struct double_pair r; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); r = randmatstat(1000); t = clock_now()-t; if (t < tmin) tmin = t; } // assert(0.5 < r.s1 && r.s1 < 1.0 && 0.5 < r.s2 && r.s2 < 1.0); print_perf("rand_mat_stat", tmin); // rand mat mul tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); double *C = randmatmul(1000); assert(0 <= C[0]); free(C); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("rand_mat_mul", tmin); // printfd tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); printfd(100000); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("printfd", tmin); return 0; }
int main() { // Initialize RNG dsfmt_gv_init_gen_rand(0); double t, tmin; // fib(20) assert(fib(20) == 6765); int f = 0; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); f += fib(20); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("fib", tmin); // parse_bin tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); char s[11]; for (int k=0; k<1000; ++k) { uint32_t n = dsfmt_gv_genrand_uint32(); sprintf(s, "%x", n); uint32_t m = (uint32_t)parse_int(s, 16); assert(m == n); } t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("parse_int", tmin); // // array constructor // tmin = INFINITY; // for (int i=0; i<NITER; ++i) { // t = clock_now(); // double *a = ones(200,200); // free(a); // t = clock_now()-t; // if (t < tmin) tmin = t; // } // print_perf("ones", tmin); // // // A*A' // //SUBROUTINE DGEMM(TRANSA,TRANSB,M,N,K,ALPHA,A,LDA,B,LDB,BETA,C,LDC) // double *b = ones(200, 200); // tmin = INFINITY; // for (int i=0; i<NITER; ++i) { // t = clock_now(); // double *c = matmul_aat(200, b); // free(c); // t = clock_now()-t; // if (t < tmin) tmin = t; // } // free(b); // print_perf("AtA", tmin); // mandel int mandel_sum; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); mandel_sum = mandelperf(); t = clock_now()-t; if (t < tmin) tmin = t; } assert(mandel_sum == 14719); print_perf("mandel", tmin); // sort tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); double *d = myrand(5000); quicksort(d, 0, 5000-1); free(d); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("quicksort", tmin); // pi sum double pi; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); pi = pisum(); t = clock_now()-t; if (t < tmin) tmin = t; } assert(fabs(pi-1.644834071848065) < 1e-12); print_perf("pi_sum", tmin); // rand mat stat struct double_pair r; tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); r = randmatstat(1000); t = clock_now()-t; if (t < tmin) tmin = t; } // assert(0.5 < r.s1 && r.s1 < 1.0 && 0.5 < r.s2 && r.s2 < 1.0); print_perf("rand_mat_stat", tmin); // rand mat mul tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); double *C = randmatmul(1000); assert(0 <= C[0]); free(C); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("rand_mat_mul", tmin); // printfd tmin = INFINITY; for (int i=0; i<NITER; ++i) { t = clock_now(); printfd(100000); t = clock_now()-t; if (t < tmin) tmin = t; } print_perf("printfd", tmin); return 0; }
double uptime_up(void) { uptime_init(); return clock_now() - uptime_booted(); }
void _time_init(void) { clock_start = clock_now(); }
int main(int argc, char **argv) { FILE *nl; char *stub; ASL *asl; fint nerror = 0; real *X; real objVal; int i, j, k, je; real* J; cgrad *cg; char *fmt; if (argc < 2) { printf("Usage: %s stub\n", argv[0]); return 1; } double t0 = clock_now(); asl = ASL_alloc(ASL_read_fg); stub = argv[1]; nl = jac0dim(stub, (fint)strlen(stub)); fg_read(nl,0); J = (real *)Malloc(nzc*sizeof(real)); X = (real *)Malloc(n_var*sizeof(real)); for (i = 0; i < n_var; i++) X[i] = 1.0; // include one jacobian call for consistency jacval(X, J, &nerror); t0 = clock_now() - t0; //objVal = objval(0, X, &nerror); //printf("Objective %.5f\n", objVal); double jactime = 1e30; for (k = 0; k < 10; k++) { double t1 = clock_now(); jacval(X, J, &nerror); t1 = clock_now() - t1; jactime = (t1 < jactime) ? t1 : jactime; } double norm = 0; for (i = 0; i < nzc; i++) { norm += J[i]*J[i]; } norm = sqrt(norm); char *bname = basename(argv[1]); // Initialization time, 1 jacobian evaluation printf("### %s %f %g\n",bname,t0,jactime); printf("## %s Jacobian norm: %.10g (nnz = %d)\n",bname,norm,nzc); /*printf("nzc %d\n",nzc); for(i = 0; i < nzc; i++) { printf("%d %g\n",i,J[i]); }*/ /* for(i = 0; i < n_con; i++) { printf("\nRow %d:", i+1); //fmt = " J[%d]=%g*X%d"; //for(cg = Cgrad[i]; cg; cg = cg->next, fmt = " + J[%d]=%g*X%d") { // printf(fmt, cg->goff, J[cg->goff], cg->varno+1); //} fmt = " %g*X%d"; for(cg = Cgrad[i]; cg; cg = cg->next, fmt = " + %g*X%d") { printf(fmt, J[cg->goff], cg->varno+1); } printf("\n"); }*/ return 0; }