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

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...
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 *...
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...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
Write a C++ die roller program. We need you to write a program that will roll...
Write a C++ die roller program. We need you to write a program that will roll dice for them, but not just six-sided dice. Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D. n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1. x may or may not be present. If it...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT