, , , , , , ,

[SOLVED] Cosc407 a2 (48 marks) focus: c pointers and functions, intro to openmp (a)

$25

File Name: Cosc407_a2__48_marks___focus__c_pointers_and_functions__intro_to_openmp__a_.zip
File Size: 706.5 KB

5/5 - (1 vote)

[12 marks] Write a C function that sequentially adds 2 vectors A and B as . Use this function header: void addVec(int* C, int* A, int* B, int size), where size is the number of elements in a vector. You should not create any additional local variables in the function (hint: use pointers and pointer expressions).

Test your function with two vectors, each having 50 million integers, all initialized to 0 (hint: calloc). Note that memory allocation for A, B, and C is done within your test code (the main function). Print the first 10 elements of C as well as the total time the function took to complete. Report the results as a comment in your code.

Sample run 1: (successful)

0 0 0 0 0 0 0 0 0 0

Execution time: 281.0 ms

Sample run 2: (unsuccessful memory allocation)

Not enough memory.

Marking: +6 for the function, +5 for the test code (2 marks for error checking), +1 for the comment)

 

 

Marking: +6 for the function (2 for error checking), +5 for the test code(2 for error checking), +1 for the comment

 

Marking: +4 for running code

 

int* vecCreate (int size)

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 Version1 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 OpenMP 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.

 

For this assignment, you need to do the following:

Note that you can resubmit an assignment, but the new submission overwrites the old submission and receives a new timestamp.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Cosc407 a2 (48 marks)  focus: c pointers and functions, intro to openmp (a)[SOLVED] Cosc407 a2 (48 marks) focus: c pointers and functions, intro to openmp (a)
$25