int main() { unsigned long start, finish, k; process_stage = processes; start = b_get_timercounter(); // Grab the starting time // Spawn the worker processes for (k=0; k<processes; k++) { b_smp_enqueue(&prime_process); } // Attempt to run a process on this CPU Core while (b_smp_queuelen() != 0) // Check the length of the queue. If greater than 0 then try to run a queued job. { local = b_smp_dequeue(); // Grab a job from the queue. b_smp_dequeue returns the memory address of the code if (local != 0) // If it was set to 0 then the queue was empty b_smp_run(local); // Run the code } // No more jobs in the queue b_smp_wait(); // Wait for all CPU cores to finish finish = b_get_timercounter(); // Grab the finish time // Print the results b_int_to_string(primes, tstring); // Print the amount of primes for verification b_print_string(tstring); b_print_string(" in "); finish = (finish - start) / 8; b_int_to_string(finish, tstring); b_print_string(tstring); b_print_string(" seconds\n"); return 0; }
int main(int argc, char *argv[]) { printf("PrimeSMP v1.6\n"); if ((argc == 1) || (argc >= 4)) { printf("usage: %s max_processes max_number\n", argv[0]); return 1; } else { max_processes = atoi(argv[1]); max_number = atoi(argv[2]); } if ((max_processes == 0) || (max_number == 0)) { printf("Invalid argument(s).\n"); printf("usage: %s max_processes max_number\n", argv[0]); return 1; } printf("Using a maximum of %ld process(es). Searching up to %ld.\n", max_processes, max_number); for (processes=1; processes <= max_processes; processes*=2) { primes = 1; process_stage = processes; #ifdef BAREMETAL unsigned long tval = 0; #else pthread_t worker[processes]; #endif printf("Processing with %ld process(es)...\n", processes); time(&start); // Grab the starting time // Spawn the worker processes for (k=0; k<processes; k++) { #ifdef BAREMETAL b_smp_enqueue(prime_process, tval); #else pthread_create(&worker[k], NULL, prime_process, NULL); #endif } #ifdef BAREMETAL // Attempt to run a process on this CPU Core do { local = b_smp_dequeue(NULL); // Grab a job from the queue. b_smp_dequeue returns the memory address of the code if (local != 0) // If it was set to 0 then the queue was empty b_smp_run(local, tval); // Run the code } while (local != 0); // Abandon the loop if the queue was empty b_smp_wait(); // Wait for all CPU cores to finish #else for (k=0; k<processes; k++) { pthread_join(worker[k], NULL); } #endif time(&finish); // Grab the finishing time // Print the results if (processes == 1) { singletime = difftime(finish, start); speedup = 1; } else { speedup = singletime / difftime(finish, start); } printf("%ld in %.0lf seconds. Speedup over 1 process: %.2lfX of maximum %ld.00X\n", primes, difftime(finish, start), speedup, processes); } return 0; }