int compare_check_and_reset_flag(compare_regs_t* const compare) { spmc_compare_t* comp = &sim_compares[(int_ptr) compare]; spmc_timer_t* tim = &sim_timers[(int_ptr) compare]; struct timeval now; gettimeofday(&now, NULL); long ticks = timediff_ms(&now, &comp->last); ticks /= (1 << tim->prescaler); if (ticks == 0) return 0; comp->last = now; if (ticks > tim->limit) { //if timer made more ticks than limit compare is always true return 1; } else { int tim_val = timediff_ms(&comp->last, &tim->start); tim_val /= (1 << tim->prescaler); tim_val += ticks; tim_val %= tim->limit; if (comp->comp_val > tim_val) { return (tim_val + ticks) > comp->comp_val; } else { return (tim_val - ticks) < comp->comp_val; } } }
void loop() { struct timeval clock_now; gettimeofday(&clock_now, NULL); chip8.emulateCycle(); if (chip8_draw_flag) { draw(); chip8_draw_flag = false; } if (timediff_ms(&clock_now, &clock_prev) >= CLOCK_RATE_MS) { chip8.tick(); clock_prev = clock_now; } }
/** * Main driver code for the parallel lab. Generates the matrix of the * specified size, initiates the decomposition and checking * routines. */ int main (int argc, char *argv[]) { int size = 0; double *a = NULL; double *lu = NULL; clock_t start, time1, time2; struct timeval start_timeval, end_timeval; double elapsed_secs, elapsed_total_secs, cpu_secs; /* Bail out if we don't have the correct number of parameters */ if (argc!=2) { printf("This program is used to decompose a (random) matrix A into its components L and U.\n"); printf("Usage: %s <matrix size>\n", argv[0]); return -1; } size = atoi(argv[1]); /* Adjust matrix size */ if (size < MIN_MATRIX_SIZE) { printf("Setting matrix size to minimum value %d.\n", MIN_MATRIX_SIZE); size = MIN_MATRIX_SIZE; } else if (size > MAX_MATRIX_SIZE) { printf("Setting matrix size to maximum value %d.\n", MAX_MATRIX_SIZE); size = MAX_MATRIX_SIZE; } /* Generate data. */ printf("LU matrix decomposition, starting warmup...\n"); printf(" - Generating a %i * %i matrix\n", size, size); a = (double*)malloc(sizeof(double)*size*size); lu = (double*)malloc(sizeof(double)*size*size); if (a==NULL || lu==NULL) { printf("Not enough memory!\n"); return -1; } fill_matrix(a, size); print_matrix(a, size); memcpy(lu, a, sizeof(double)*size*size); /* Start LU decomposition. */ printf("Decomposing the matrix into its components...\n"); gettimeofday(&start_timeval, NULL); start = clock(); decompose_matrix(lu, size); time1 = clock()-start; gettimeofday(&end_timeval, NULL); elapsed_total_secs = elapsed_secs = (double)timediff_ms(&start_timeval, &end_timeval)/1000.0; cpu_secs = (double)(time1)/CLOCKS_PER_SEC; /* Verify resulting decomposition. */ printf("Checking result...\n"); print_matrix(lu, size); gettimeofday(&start_timeval, NULL); start = clock(); if (check_matrix(lu, a, size)) printf("The computation seems correct\n"); else printf("The computation seems not correct\n"); time2 = clock()-start; gettimeofday(&end_timeval, NULL); /* Output stats. */ printf("\nDecomposition time: %.2fs CPU, %.2fs elapsed, %.1f%% speedup\n", cpu_secs, elapsed_secs, cpu_secs/elapsed_secs*100.0); elapsed_secs = (double)timediff_ms(&start_timeval, &end_timeval)/1000.0; elapsed_total_secs += elapsed_secs; cpu_secs = (double)(time2)/CLOCKS_PER_SEC; printf("Checking time: %.2fs CPU, %.2fs elapsed, %.1f%% speedup\n", cpu_secs, elapsed_secs, cpu_secs/elapsed_secs*100.0); cpu_secs = (double)(time1+time2)/CLOCKS_PER_SEC; printf("Overall time: %.2fs CPU, %.2fs elapsed, %.1f%% speedup\n", cpu_secs, elapsed_total_secs, cpu_secs/elapsed_total_secs*100.0); /* Free resources. */ free(lu); free(a); return 0; }