0% found this document useful (0 votes)
8 views5 pages

S24 PDC Mid Exam Solution

Parallel And distrubuted computing

Uploaded by

abbastayyaba417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

S24 PDC Mid Exam Solution

Parallel And distrubuted computing

Uploaded by

abbastayyaba417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Clulbersity of Central Wunjab Faculty oF tofupmuation Technolugy Parattel ang Distributed Computing Mid Terms Sprit Registration Nomne=___——__ ‘Duration: Seat 1362 polatsy at serv of cache local Assure 10 Moe Order is achievable in ye fallowing code tod how would you obtain it? erooty fer he aay.) Tor Gt: jem; jay For (let; om ies ALLL = AC LOT + ayy, Solution: The type of cache locality that és achievable ia the provided code is spatial locality. Spatial Jocality refers t0 the tse Of data elements within relatively close storage lucations. The given code accesses actay elements in a eolumnemajar order dve 10 the arrangement of the Loop indices. Tw improve spatial locality and parentially enkance cache performances. you should switelt the onder of the loops to secess the elements in a row-majar order: for (=O: 1g tes for b=) < Milla) = Abii) * BULL by Wrtea cade using » loop mevich cout be pacallesed? nie Solution: An example of a loop that cannot be | paralized effectively th one dist ‘The code caleulates the sum of rumbers contains depentlencics from ane iteration fave} ton wsing «Loup to the next, such as calculating the In each iterstion. the current value of i Fibonacci sequence: is added to the sum, 7. . ©) This calculation depends on the previous tt Ont) Value of sum, For example, to calculate the for int d= 2; 1< m3 9) ( sum of: J the sum of the first fib2 = fibl + fib; fit = fit: fibt = fib’ using namespace st: int maing) | sun > i: Dependent an previ value int n> 10:// Number af elements fee int sum © 0 t Loop to caleulate the sum of clements SSeS ie Se Sui Sele from 1 to n (serial) telumn O; for (int i ple) ‘ 2 (3:2 paints) ‘SMG of a progratn’s execution time aecurs inside a lvop thst ea.a he executed in paralel and ress 40% in Serial 3) What s the maximum speedup we should expect from a parallel version of the progr executiny of core CPU hb) Ifwe wanted toachleve 4 time speedup how may processing tinity will ke required. is it prnsible gain such speedip? Ps the proportion of the progeam that can be parallelizer! (0,6 in this case), Nis the number af processors (@ cores in this case} Sphinn bts. b) Achieving 4s Speedup: This speedup can't be achieved, according to. amdeht's lav; It can’t ga beyond 2 The N value should be negative instead of 6 it should Ge The answer was "this valut C201 be achieved because if we put Ninf we will get lesser Value then & ; 18 pants) a The army must be declared and initialized jn the main thread. You may initial rand) fumetion. =include include sinelude #define ARRAY_SIZE 1000. int arr| ARRAY_SIZE]; // Array to store elements (1000 elements) "int sum = 0; // Global variable to store the final sum /! Mutex for thread synchronization pthread_mutex_t muten / Thread function to calculate partial sum void *sum_array(void *arg) { int start = *(int *)arg: #f Get starting index from argument int end = start +(ARRAY_SIZE/N); — // Calculate ending index int partial_sum = 0; / Variable for partial sum for (int i = start; i < end; i++) { partial_sum += arr[i); # Add element to partial sum ‘ ‘ pthread_mutex_lock(&mutex); /! Acquire lock for shared variable sum += partial_sum: 4 Add partial sum to global sum pthread_mutex_unlock(&mutex ——_// Release lock after modification free(arg); // Free memory allocated for start_index return NULL; > s int main(int arge, char *arev[]) Purge | prinut("Usay \n", argy]O)); return 1; evi) sranditime( NULL): ARRAY_SIZE; i++) | int N = atoil Get number of threads from command line for (inti = 05 arrfi] = rand() % 100; // Initialize array with random values puhread_t threads[N}: / Array to store thread handles pthread_mutes_init(&mutex, NULL): // Initialize mutex for Ginti=0;1

You might also like