Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//how do i parallelize the linear search for chunk and mod for serial and parall

ID: 3910682 • Letter: #

Question

 //how do i parallelize the linear search for chunk and mod for serial and parallel //i have it declared but dont know how to parallelize them c++ Parallel Programming  #include <iostream> #include <omp.h> #include <fstream> #include <string>  using namespace std;  enum METHOD { Chunk, Mod };  int serialLinear(int *, int, int, int, METHOD); int parallelLinear(int *, int, int, int, METHOD); int serialLinearBreaking(int *, int, int, int, METHOD);  int main() {         int sizes[] = { 10000,100000,1000000 };         int numThreads[] = { 1,2,4,8,16 };         int * arr;          ofstream out[6];         string outFileName;          for (int i = 0; i < 3; i++) {                 arr = new int[sizes[i]];                 for (int j = 0; j < sizes[i]; j++) {                         arr[j] = j;                 }                 for (int j = 0; j < 5; j++) { // thread run                         outFileName = "size"+to_string(sizes[i])+"threads"+to_string(numThreads[j]);                                                  out[0].open(outFileName + "serialChunked.txt");                         out[1].open(outFileName + "serialMod.txt");                         out[2].open(outFileName + "serialBreakingChunked.txt");                         out[3].open(outFileName + "serialBreakingMod.txt");                         out[4].open(outFileName + "parallelChunked.txt");                         out[5].open(outFileName + "parallelMod.txt");                          for (int item = 0; item < sizes[i]; item += sizes[i] * .001) {                                  double start, end;                                 //Insert functions after start;                                  //serialChunked;                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[0] << item << " " << end - start;                                  //serialMod                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[1] << item << " " << end - start;                                  //serialBreakingChunked                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[2] << item << " " << end - start;                                  //serialBreakingMod                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[3] << item << " " << end - start;                                  //parallelChunked                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[4] << item << " " << end - start;                                  //parallelMod                                 start = omp_get_wtime();                                  end = omp_get_wtime();                                 out[5] << item << " " << end - start;                         }                          out[0].close();                         out[1].close();                         out[2].close();                         out[3].close();                         out[4].close();                         out[5].close();                                  }                 delete[] arr;         }         return 0; }  int serialLinear(int * arr, int size, int NT, int item, METHOD med) {         int index = -1;         return index; }  int parallelLinear(int * arr, int size, int NT, int item, METHOD med) {         int index = -1;         return index; }  int serialLinearBreaking(int * arr, int size, int NT, int item, METHOD med) {         int index = -1;         return index; }

Explanation / Answer

/*   We can parallelize for loops by adding following statement
    above the for loop

    #pragma omp parallel -------------(1)


    The biggst running loop in the program is

    for (int item = 0; item < sizes[i]; item += sizes[i] * .001)

    So better to add parallelism to the for loop. Just place (1) above this for loop.

    We can also set the number of threads with

    omp_set_num_threads(n);
    This should be called before (1)
   
*/

#include <iostream>
#include <omp.h>
#include <fstream>
#include <string>

using namespace std;

enum METHOD { Chunk, Mod };

int serialLinear(int *, int, int, int, METHOD);
int parallelLinear(int *, int, int, int, METHOD);
int serialLinearBreaking(int *, int, int, int, METHOD);

int main() {
        int sizes[] = { 10000,100000,1000000 };
        int numThreads[] = { 1,2,4,8,16 };
        int * arr;

        ofstream out[6];
        string outFileName;

        for (int i = 0; i < 3; i++) {
                arr = new int[sizes[i]];
                for (int j = 0; j < sizes[i]; j++) {
                        arr[j] = j;
                }
                for (int j = 0; j < 5; j++) { // thread run
                        outFileName = "size"+to_string(sizes[i])+"threads"+to_string(numThreads[j]);
                       
                        out[0].open(outFileName + "serialChunked.txt");
                        out[1].open(outFileName + "serialMod.txt");
                        out[2].open(outFileName + "serialBreakingChunked.txt");
                        out[3].open(outFileName + "serialBreakingMod.txt");
                        out[4].open(outFileName + "parallelChunked.txt");
                        out[5].open(outFileName + "parallelMod.txt");

                        for (int item = 0; item < sizes[i]; item += sizes[i] * .001) {

                                double start, end;
                                //Insert functions after start;

                                //serialChunked;
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[0] << item << " " << end - start;

                                //serialMod
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[1] << item << " " << end - start;

                                //serialBreakingChunked
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[2] << item << " " << end - start;

                                //serialBreakingMod
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[3] << item << " " << end - start;

                                //parallelChunked
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[4] << item << " " << end - start;

                                //parallelMod
                                start = omp_get_wtime();

                                end = omp_get_wtime();
                                out[5] << item << " " << end - start;
                        }

                        out[0].close();
                        out[1].close();
                        out[2].close();
                        out[3].close();
                        out[4].close();
                        out[5].close();                
                }
                delete[] arr;
        }
        return 0;
}

int serialLinear(int * arr, int size, int NT, int item, METHOD med)
{
        int index = -1;
        return index;
}

int parallelLinear(int * arr, int size, int NT, int item, METHOD med)
{
        int index = -1;
        return index;
}

int serialLinearBreaking(int * arr, int size, int NT, int item, METHOD med)
{
        int index = -1;
        return index;
}