Created
January 26, 2011 01:11
-
-
Save PlloYNiiE/796036 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /////////////////////////////////////////////////////////////////////////////// | |
| // | |
| // GetCommandLineArguments | |
| // | |
| // Grabs the number of threads from the commandline | |
| // | |
| /////////////////////////////////////////////////////////////////////////////// | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <omp.h> | |
| #include <time.h> | |
| #include <math.h> | |
| #include <windows.h> | |
| static int gProgress = 0, | |
| gPrimesFound = 0; | |
| long globalPrimes[1000000]; | |
| int CLstart, CLend; | |
| int gstart, gend; | |
| CRITICAL_SECTION g_cs; | |
| HANDLE hSem1; | |
| #define numThreads 4 | |
| void GetCommandLineArguments(int argc, char **argv, int &start, int &end) { | |
| if( argc == 3 ) { | |
| CLstart = atoi(argv[1]); | |
| CLend = atoi(argv[2]); | |
| } | |
| else { | |
| printf("Usage:- %s <start range> <end range>\n", argv[0]); | |
| exit(-1); | |
| } | |
| if (CLstart > CLend) { | |
| printf("Start value must be less than or equal to end\n"); | |
| exit(-1); | |
| } | |
| if (CLstart < 1 && CLend < 2) { | |
| printf("Range must be positive integers\n"); | |
| exit(-1); | |
| } | |
| start = CLstart; | |
| end = CLend; | |
| if (CLstart < 2) start = 2; | |
| if (start <= 2) globalPrimes[gPrimesFound++] = 2; | |
| } | |
| void ShowProgress( int val, int range ) { | |
| int percentDone = 0; | |
| WaitForSingleObject(hSem1, INFINITE); // access to input | |
| gProgress++; | |
| ReleaseSemaphore(hSem1, 1, NULL); | |
| percentDone = (int)((float)gProgress/(float)range *200.0f + 0.5f); | |
| if ( percentDone % 10 == 0 ) { | |
| printf("\b\b\b\b%3d%%", percentDone); | |
| } | |
| } | |
| bool TestForPrime(int val) { | |
| int limit, factor = 3; | |
| limit = (long)(sqrtf((float)val)+0.5f); | |
| while( (factor <= limit) && (val % factor)) | |
| factor ++; | |
| return (factor > limit); | |
| } | |
| DWORD WINAPI FindPrimes(LPVOID pArg) { | |
| int myNum = *((int*)pArg), | |
| i, | |
| local_start = myNum*(int)ceil((double)(gend/numThreads)); | |
| int local_end = myNum*(int)floor((double)(gend/numThreads)) + (int)ceil((double)(gend/numThreads)); | |
| if (myNum==0) { | |
| local_start = gstart; | |
| } | |
| if ((local_start % 2) == 0 ) { | |
| local_start = local_start + 1; // ensure we start with an odd number | |
| } | |
| if (myNum==numThreads-1 ) { | |
| local_end = (int)gend; | |
| } | |
| for(i=local_start; i<local_end; i+=2) { | |
| if (TestForPrime(i)) { | |
| EnterCriticalSection(&g_cs); | |
| globalPrimes[gPrimesFound++] = i; | |
| LeaveCriticalSection(&g_cs); | |
| } | |
| //ShowProgress(i, end); | |
| } | |
| return 0; | |
| } | |
| int main(int argc, char **argv) { | |
| int i,tnum[numThreads]; | |
| clock_t before, after; | |
| GetCommandLineArguments(argc, argv, gstart, gend); | |
| HANDLE hThread[numThreads]; | |
| hSem1 = CreateSemaphore(NULL, 1, 1, NULL) | |
| before = clock(); | |
| InitializeCriticalSection(&g_cs); | |
| for ( i = 0; i < numThreads; i++ ){ | |
| tnum[i] = i; | |
| hThread[i] = CreateThread(NULL, 0, FindPrimes, &tnum[i], 0, NULL); | |
| } | |
| WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE); | |
| DeleteCriticalSection(&g_cs); | |
| after = clock(); | |
| printf("\n\n%8d primes found between %6d and %6d in %7.2f secs\n", | |
| gPrimesFound, | |
| CLstart, CLend, (float)(after - before)/ CLOCKS_PER_SEC); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment