Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write CODE. Please help. Performance measurement of a program with multiple thre

ID: 3589522 • Letter: W

Question

Write CODE.

Please help.

Performance measurement of a program with multiple threads Let's implement a general matrix multiplication with use of system calls: gettimeofday and getrusage. You will run the program for N (matrix size) -1000, 2000, and 4000, respectively to fill up the table in milliseconds: The matrix dimension is N rows by N columns. You will create M multithreads to do the multiplication, dividing the job by rows or columns. and m will be the number of threads to be created The matrix element (ij) will be of type "double", initially setting to (double)rand()/RAND_MAX+1; Your program will take option "-N" and"-M" as inputs, "yourmul-N n-M m, where n will be the matrix size If you run your program multiple times for the same value of N, pick one of them or avg to fill in the table a) For M=1 (in milliseconds): Elapsed Time (30%) USR Time SYS Time USR+SYS Time 4000 b) (25%) For M=2 (in milliseconds Elapsed Time USR Time SYS Time USR+SYS Time 4000 c) (25%) For M-4 (in milliseconds Elapsed Time USR Time SYS Time USR+SYS Time 1000 d) (10%) By checking your system information from "/proc/cpuinfo", list # of cores, its clock speed, and load e) (10%) Compare "Elapsed Time" and "USR-SYS Time" from 5a) and 5c) for N-4000. Discuss your findings. Here is a brief explanation about time & clocks. average (use "uptime") Wall Clock time -the amount of time the process takes to run, sometimes called elapsed time User (CPU) time the CPU time attributed to user instructions System (CPU) time the CPU time attributed to the kernel when it executes on behalf of the process

Explanation / Answer

#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

#define SIZE 10   // Size by SIZE matrices
int num_thrd;   // number of threads

int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];

// initialize a matrix
void init_matrix(int m[SIZE][SIZE])
{
int i, j, val = 0;
for (i = 0; i < SIZE; i++)
    for (j = 0; j < SIZE; j++)
      m[i][j] = val++;
}

void print_matrix(int m[SIZE][SIZE])
{
int i, j;
for (i = 0; i < SIZE; i++) {
    printf(" | ");
    for (j = 0; j < SIZE; j++)
      printf("%2d ", m[i][j]);
    printf("|");
}
}

// thread function: taking "slice" as its argument
void* multiply(void* slice)
{
int s = (int)slice;   // retrive the slice info
int from = (s * SIZE)/num_thrd; // note that this 'slicing' works fine
int to = ((s+1) * SIZE)/num_thrd; // even if SIZE is not divisible by num_thrd
int i,j,k;

printf("computing slice %d (from row %d to %d) ", s, from, to-1);
for (i = from; i < to; i++)
{
    for (j = 0; j < SIZE; j++)
    {
      C[i][j] = 0;
      for ( k = 0; k < SIZE; k++)
C[i][j] += A[i][k]*B[k][j];
    }
}
printf("finished slice %d ", s);
return 0;
}

int main(int argc, char* argv[])
{
pthread_t* thread; // pointer to a group of threads
int i;

if (argc!=2)
{
    printf("Usage: %s number_of_threads ",argv[0]);
    exit(-1);
}

num_thrd = atoi(argv[1]);
init_matrix(A);
init_matrix(B);
thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));

// this for loop not entered if threadd number is specified as 1
for (i = 1; i < num_thrd; i++)
{
    // creates each thread working on its own slice of i
    if (pthread_create (&thread[i], NULL, multiply, (void*)i) != 0 )
    {
      perror("Can't create thread");
      free(thread);
      exit(-1);
    }
}

// main thread works on slice 0
// so everybody is busy
// main thread does everything if threadd number is specified as 1
multiply(0);

// main thead waiting for other thread to complete
for (i = 1; i < num_thrd; i++)
pthread_join (thread[i], NULL);

printf(" ");
print_matrix(A);
printf("        * ");
print_matrix(B);
printf("        = ");
print_matrix(C);
printf(" ");

free(thread);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote