Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	if ((argc == 1) || (argc >= 4))
	{
		printf("usage: %s max_processes max_number\n", argv[0]);
		exit(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]);
		exit(1);
	}

	printf("PrimeSMP v1.4 - Using a maximum of %ld process(es). Searching up to %ld.\n", max_processes, max_number);

	unsigned long k;

	for (processes=1; processes <= max_processes; processes*=2)
	{
		primes = 1;
		process_stage = processes;

#ifdef BAREMETAL
		unsigned long start, finish;
		start = b_get_timercounter();		// Grab the starting time
#else
		time_t start, finish;
		time(&start);				// Grab the starting time
		pthread_t worker[processes];
#endif

		// Spawn the worker processes
		for (k=0; k<processes; k++)
		{
#ifdef BAREMETAL
			b_smp_enqueue(&prime_process);
#else
			pthread_create(&worker[k], NULL, prime_process, NULL);
#endif
		}

		printf("Processing with %ld process(es)...\n", processes);

#ifdef BAREMETAL
		// 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
		}
		b_smp_wait();				// Wait for all CPU cores to finish
#else
		for (k=0; k<processes; k++)
		{
			pthread_join(worker[k], NULL);
		}
#endif

		// Print the results
#ifdef BAREMETAL
		finish = b_get_timercounter();
#else
		time(&finish);
		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);
#endif

	}

	return 0;
}