Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q4. [20 marks] Develop a C function that returns an int* pointer to a vector A initialized as A[i] = i, or returns NULL if
Q4. [20 marks] Develop a C function that returns an int* pointer to a vector A initialized as A[i] = i, or returns NULL if the function fails. Write the sequential code for the function. Use the header: int* vecCreate (int size) Write the Open MP implementation of the function. Use the header: int* vecCreateOpenMP(int size, int num_thread) Here, you will try to speed up the vector initialization by dividing the work among several threads where each thread will initialize a segment of the vector. For example, if num_thread = 2 and size = 10, thread 0 will initialize elements 0 to 4 and thread 1 will initialize elements 5 to 9. Using #pragma omp parallel num_threads( num_thread ). Your code should be similar to Version 1 of the Trapezoid Area Calculation example from the lecture notes. Your function should only work if the vector size is divisible by the number of threads. If not, your function should display an error message and return NULL. Test both functions with a 50-million element vector (and 4 threads for the second function). Your test code should print the value of the last element in the vector along with the time taken for each function to complete. Report the results as a comment in your code. Sample run 1: (successful) Using serial code V[49999999] = 49999999 Time: 144.00 ms Using OpenMP with 4 threads: V[49999999] = 49999999 Time: 59.00 ms Sample run 2: (num_thread not divisible by size) Using serial code V[49999999] = 49999999 Time: 144.00 ms Using Openvp with 3 threads: Error: number of threads must be divisible by vector size. Sample run 3: (unsuccessful memory allocation) Not enough memory. Not enough memory. Marking: +5 for serial code, +10 for parallel code, +5 for the test code. Up to -4 marks for not handling errors
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started