int main(void) { InitSys(); // Get User info, initialize arrays GenTables(); // Generate the base to power hash tables StartCalcsTime = GetDecTime(); // Start multiple threads. #pragma omp parallel num_threads(GlobalNbrThreads) { // Each thread will have its own copy of these long long i, j, k, KendsAt; unsigned ImodPrime1, ImodPrime2, SumModPrime1, SumModPrime2; #pragma omp for schedule(dynamic, GlobalChunkSize) // When you use pragma "for", the system will calculate local // ranges for each copy of the "for" loop. As a programmer, just // write the code as if only 1 core is operating. // Search the tables. For each record, get the sum. See // if there is some other record that matches this sum. // For user range within the X to the Y table. for (i = StartMainSearch; i <= EndMainSearch; i++) { // This is the A^X loop if (Power[i] < 10) // Power is shifted 1 bit to left, so test is actually: if (power < 5) continue; // Only process rest if power is >= 5 ImodPrime1 = X2YmodPrime1[i]; // Get modulos for I ImodPrime2 = X2YmodPrime2[i]; for (j = NbrRecords; j; j--) { // For all j down to 1. (This is the B^Y loop) SumModPrime1 = X2YmodPrime1[j] + ImodPrime1; if (SumModPrime1 >= PRIME1) SumModPrime1 -= PRIME1; SumModPrime2 = X2YmodPrime2[j] + ImodPrime2; // 2nd modulo sum if (SumModPrime2 >= PRIME2) SumModPrime2 -= PRIME2; KendsAt = SortHeads[SumModPrime1 + 1] - 1; // Construct KendsAt for (k = SortHeads[SumModPrime1] ; k <= KendsAt; k++) { // Note: The mean number of iterations in this loop is 2. // Thus it runs in linear time. if (SumModPrime2 != X2YmodPrime2[k]) // If not pass modulo tests, continue. continue; // Will "continue" 99.9999+% of the time // Only one thread at a time for called routines. #pragma omp critical { // Else check A^X + B^Y = C^Z full sums for possible solution GlobalThreadID = omp_get_thread_num(); // "Critical" (Lots of calcs and don't Check4Solution(i, j, k); // want interference.) } // End of critical section } // Next k } // Next j } // Next i } // End of pragma for mutiple threads if (MaxFileSize) fclose(file1); EndCalcsTime = GetDecTime(); printf("\n\nFor Upper Limit = 1.0E%d\n", Pwr10UpperLim); sprintf(DataBuff, "%lld", StartMainSearch); CommaNbr(DataBuff); // Note: CommaNbr() puts commas in whatever number is in the string array. sprintf(CommaBuff, "%lld", EndMainSearch); CommaNbr(CommaBuff); printf("For search index = %s to %s:\n", DataBuff, CommaBuff); sprintf(DataBuff, "%d", SolCount); CommaNbr(DataBuff); printf("Number of unique solutions = %s\n", DataBuff); sprintf(DataBuff, "%d", DupCount); CommaNbr(DataBuff); printf("Nbr. of additional dupl. sol. = %s\n", DataBuff); printf("\nUsing %d threads and Chunk Size = %lld:\nTotal elapsed time = %g seconds\n", GlobalNbrThreads, GlobalChunkSize, EndCalcsTime - StartCalcsTime); sprintf(DataBuff, "%.2f", (EndMainSearch - StartMainSearch + 1) / (EndCalcsTime - StartCalcsTime)); CommaNbr(DataBuff); printf("Combined threads averaged %s \"I\" per second\n", DataBuff); PauseMsg(); return 0; }