#include #include #include #include using namespace std::chrono; using namespace std; void randomVector(int vector[], int size) {     for (int i = 0; i < size; i++)     {         //ToDo: Add Comment         vector[i] = rand() % 100;     } } int main(){     unsigned long size = 100000000;     srand(time(0));     int *v1, *v2, *v3;     //ToDo: Add Comment     auto start = high_resolution_clock::now();     //ToDo: Add Comment     v1 = (int *) malloc(size * sizeof(int *));     v2 = (int *) malloc(size * sizeof(int *));     v3 = (int *) malloc(size * sizeof(int *));     randomVector(v1, size);     randomVector(v2, size);     //ToDo: Add Comment     for (int i = 0; i < size; i++)     {         v3[i] = v1[i] + v2[i];     }     auto stop = high_resolution_clock::now();     //ToDo: Add Comment     auto duration = duration_cast(stop - start);     cout << "Time taken by function: "          << duration.count() << " microseconds" << endl;     return 0;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <chrono>


using namespace std::chrono;
using namespace std;

void randomVector(int vector[], int size)
{
    for (int i = 0; i < size; i++)
    {
        //ToDo: Add Comment
        vector[i] = rand() % 100;
    }
}


int main(){

    unsigned long size = 100000000;

    srand(time(0));

    int *v1, *v2, *v3;

    //ToDo: Add Comment
    auto start = high_resolution_clock::now();

    //ToDo: Add Comment
    v1 = (int *) malloc(size * sizeof(int *));
    v2 = (int *) malloc(size * sizeof(int *));
    v3 = (int *) malloc(size * sizeof(int *));


    randomVector(v1, size);

    randomVector(v2, size);


    //ToDo: Add Comment
    for (int i = 0; i < size; i++)
    {
        v3[i] = v1[i] + v2[i];
    }

    auto stop = high_resolution_clock::now();

    //ToDo: Add Comment
    auto duration = duration_cast<microseconds>(stop - start);


    cout << "Time taken by function: "
         << duration.count() << " microseconds" << endl;

    return 0;
}

 

Please answer the below answers theoritically and not in a code form. 

1. Add the default(none)attribute to the #pargma omp parallel directive. Compile and run your code.
If you get any compilation error, try to identify the reason. As required, add any of the shared,
private or firstprivate attributes to fix the compilation error. Try different variations of data
sharing (e.g. shared(size) private(v1) or private(size) shared(v1) or ...). Is the outcome
of your program different? Explain why.
2. Compute the total sum of all the elements in v3 using a shared variable called total and atomic
update directive.
3. Use the reduction clause to compute the total sum of all the elements in v3.
4. Implement an alternative version where each thread computes its own part to a private variable and
then use a critical section (#pragma omp critical) after the loop to calculate the total sum. Do you
get the exact same results in all cases?
5. Try different OpenMP Scheduling techniques by adding schedule(type[,chunk]) attribute to
#pargma omp for directive. Experiment with the chunk size to understand how each Scheduling
technique works. Does changing the scheduling techniques or chunk size impact the execution time.
Briefly explain your observations.

Expert Solution
steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education