Question

In: Computer Science

For this assignment, you need to write a parallel program in C++ using OpenMP for vector...

For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:

#pragma omp parallel num_threads(no of threads)

The program should take the number of threads and the number of iterations from the user and divide the number of iterations between the threads. Note, if the number of iterations does not divide evenly into the number of threads your program should account for this. (Hint: If you have p threads, first (p - 1) threads should have the same input size and the last thread will take care of whatever the remainder portion.)

As an example, input vector A is initialized with its size to 10,000 and elements from 1 to 10,000. So, A[0] = 1, A[1] = 2, A[2] = 3, … , A[9999] = 10000. Vector B will be initialized to the same size with opposite inputs. So, B[0] = 10000, B[1] = 9999, B[2] = 9998, … , B[9999] = 1 Using above input vectors A and B, create output Vector C which will be computed as C[ i ] = A[ i ] + B[ i ]; You should check whether your output vector value is 10001 in every C[ i ].

Solutions

Expert Solution

#include<iostream>
#include <omp.h>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int main(){

   #pragma omp parallel

   {
       int n,thread;
       cin>>thread>>n;
       omp_set_num_threads(thread);
       int d = n%thread;
      
       vector<int> a(n);  
       vector<int> b(n);  
       vector<int> c(n);  
       int t= 1;
       for(int i=0;i<n;i++){
           a[i] = t;
           b[i] = t;
           t++;
       }
       cout<<endl;
       reverse(b.begin(), b.end());
       for(int i=0;i<n;i++){
       }
       cout<<endl;
       int check = n+1;
       for(int i=0;i<n;i++){
           c[i] = a[i]+b[i];
           if(c[i] == check){
               cout<<c[i]<<" "<<"Yes"<<endl;
           }
           else{
               cout<<c[i]<<" "<<"NO"<<endl;
           }
       }
}
  
   return 0;
}


Related Solutions

For this assignment, write a parallel program in C++ using OpenMP for vector addition. Assume A,...
For this assignment, write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. As an input vector A, initialize its size to 10,000 and elements from...
Directions: Using a vector of integers that you define. Write a C++ program to run a...
Directions: Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 1. Write a function called getValidAge that allows a user...
Using a vector of integers that you define. Write a C++ program to run a menu...
Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 2. Write a function called getValidAge that allows a user to...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Write a program: For this assignment, you will need to write three source code files as...
Write a program: For this assignment, you will need to write three source code files as well as two header files. Each of these files is relatively short, but many inexperienced programmers are overwhelmed by the idea of writing a program as multiple files. "Where do I start?!!" is a common refrain. This assignment sheet attempts to walk you through the steps of writing a multi-file program. The steps outlined below should not be thought of as a purely linear...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT