I have a personal work to complete in C programming language
Pthreads-01/Matrix-Vector-Multiplication/Pthreads-Version/Pth_Mat_Vect.c
/////////////////////////////////////////////////////////////////////////////////////// // // Purpose: This is a parallel implementation of matrix-vector multiplication. // Matrix and vector are distributed by block rows and blocks, respectively. // // Compile: gcc Pth_Mat_Vect.c -o Pth_Mat_Vect -lpthread // // Usage: ./Pth_Mat_Vect <thread_count> // // Notes: // 1. Local storage for A, x, y is dynamically allocated. // 2. Number of threads(thread_count) should evenly divide both // m and n.The program doesn't check for this. // 3. We use a 1 - dimensional array for A and compute subscripts // using the formula A[i][j] = A[i*n + j] // 4. Distribution of A, x, and y is logical : all three are // globally shared. // /////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <pthread.h> // Global variables int thread_count; int m, n; double* A; double* x; double* y; // Serial functions void Get_dims(int* m_p, int* n_p); void Read_matrix(double A[], int m, int n); void Read_vector(double x[], int n); void Print_result(double y[], int m); // Parallel function void *Pth_mat_vect(void* rank); int main(int argc, char* argv[]) { long thread; pthread_t* thread_handles; if (argc != 2) fprintf(stderr, "usage: pth_mat_vect <thread_count>\n"); thread_count = atoi(argv[1]); thread_handles = malloc(thread_count*sizeof(pthread_t)); Get_dims(&m, &n); A = malloc(m*n*sizeof(double)); x = malloc(n*sizeof(double)); y = malloc(m*sizeof(double)); Read_matrix(A, m, n); Read_vector(x, n); for (thread = 0; thread < thread_count; thread++) pthread_create(&thread_handles[thread], NULL, Pth_mat_vect, (void*) thread); for (thread = 0; thread < thread_count; thread++) pthread_join(thread_handles[thread], NULL); Print_result(y, m); free(A); free(x); free(y); return 0; } //////////////////////////////////////////////////////////////// // // Purpose: Read the dimensions of the matrix from stdin // //////////////////////////////////////////////////////////////// void Get_dims(int* m_p, int* n_p) { printf("Enter the number of rows\n"); scanf("%d", m_p); printf("Enter the number of columns\n"); scanf("%d", n_p); if (*m_p <= 0 || *n_p <= 0) { fprintf(stderr, "m and n must be positive\n"); exit(-1); } } //////////////////////////////////////////////////////////////// // // Purpose: Read the contents of the matrix from stdin // //////////////////////////////////////////////////////////////// void Read_matrix(double A[], int m, int n) { int i, j; printf("Enter the matrix A:\n"); for (i = 0; i < m; i++) for (j = 0; j < n; j++) scanf("%lf", &A[i*n + j]); } //////////////////////////////////////////////////////////////// // // Purpose: Read a vector from stdin // //////////////////////////////////////////////////////////////// void Read_vector(double x[], int n) { int i; printf("Enter the vector x:\n"); for (i = 0; i < n; i++) scanf("%lf", &x[i]); } //////////////////////////////////////////////////////////////// // // Purpose: Multiply an mxn matrix by an nx1 column vector // //////////////////////////////////////////////////////////////// void *Pth_mat_vect(void* rank) { long my_rank = (long) rank; int i, j; int local_m = m/thread_count; int my_first_row = my_rank*local_m; int my_last_row = (my_rank+1)*local_m - 1; for (i = my_first_row; i <= my_last_row; i++) { y[i] = 0.0; for (j = 0; j < n; j++) y[i] += A[i*n+j]*x[j]; } return NULL; } //////////////////////////////////////////////////////////////// // // Purpose: Print the contents of a vector to stdout // //////////////////////////////////////////////////////////////// void Print_result(double y[], int m) { int i; printf("\nThe vector y:\n"); for (i = 0; i < m; i++) printf("%f ", y[i]); printf("\n"); }
Pthreads-01/Matrix-Vector-Multiplication/Serial-Version/Serial_Mat_Vect.c
///////////////////////////////////////////////////////////////////////////////// // // Purpose: This is a serial implementation of matrix-vector multiplication // using one-dimensional arrays to store the vectors and the matrix. // // Compile: gcc Serial_Mat_Vect.c -o Serial_Mat_Vect // // Usage: ./Serial_Mat_Vect // ////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> void Get_dims(int* m_p, int* n_p); void Read_matrix(double A[], int m, int n); void Read_vector(double x[], int n); void Print_result(double y[], int m); void Mat_vect_mult(double A[], double x[], double y[], int m, int n); int main(void) { double* A = NULL; double* x = NULL; double* y = NULL; int m, n; Get_dims(&m, &n); A = malloc(m*n*sizeof(double)); x = malloc(n*sizeof(double)); y = malloc(m*sizeof(double)); if (A == NULL || x == NULL || y == NULL) { fprintf(stderr, "Can't allocate storage\n"); exit(-1); } Read_matrix(A, m, n); Read_vector(x, n); Mat_vect_mult(A, x, y, m, n); Print_result(y, m); free(A); free(x); free(y); return 0; } //////////////////////////////////////////////////////////////// // // Purpose: Read the dimensions of the matrix from stdin // //////////////////////////////////////////////////////////////// void Get_dims(int* m_p, int* n_p) { printf("Enter the number of rows\n"); scanf("%d", m_p); printf("Enter the number of columns\n"); scanf("%d", n_p); if (*m_p <= 0 || *n_p <= 0) { fprintf(stderr, "m and n must be positive\n"); exit(-1); } } //////////////////////////////////////////////////////////////// // // Purpose: Read the contents of the matrix from stdin // //////////////////////////////////////////////////////////////// void Read_matrix(double A[], int m, int n) { int i, j; printf("Enter the matrix A:\n"); for (i = 0; i < m; i++) for (j = 0; j < n; j++) scanf("%lf", &A[i*n+j]); } //////////////////////////////////////////////////////////////// // // Purpose: Read a vector from stdin // //////////////////////////////////////////////////////////////// void Read_vector(double x[], int n) { int i; printf("Enter the vector x:\n"); for (i = 0; i < n; i++) scanf("%lf", &x[i]); } //////////////////////////////////////////////////////////////// // // Purpose: Print the contents of a vector to stdout // //////////////////////////////////////////////////////////////// void Print_result(double y[], int m) { int i; printf("\nThe vector y:\n"); for (i = 0; i < m; i++) printf("%f ", y[i]); printf("\n"); } //////////////////////////////////////////////////////////////// // // Purpose: Multiply a matrix by a vector // //////////////////////////////////////////////////////////////// void Mat_vect_mult(double A[], double x[], double y[], int m, int n) { int i, j; for (i = 0; i < m; i++) { y[i] = 0.0; for (j = 0; j < n; j++) y[i] += A[i*n+j]*x[j]; } }
Pthreads-01/Hello-World/Version-01/Pth_Hello_1.c
////////////////////////////////////////////////////////////////////////// // // Purpose: Create a single thread and print a message. // // Compile: gcc Pth_Hello_1.c -o Pth_Hello_1 -lpthread // // Usage: ./Pth_Hello_1 // /////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void * Hello_Fun() { printf("Hello World!\n"); return NULL; } int main(int argc, char * argv[]) { pthread_t thread; //Thread identifier //Create a new thread to have it run the function Hello_Fun pthread_create(&thread, NULL, Hello_Fun, NULL); //Wait until the thread completes pthread_join(thread, NULL); return 0; }
Pthreads-01/Hello-World/Version-02/Pth_Hello_2.c
////////////////////////////////////////////////////////////////////////// // // Purpose: Create some threads, each of which prints a message. // // Compile: gcc Pth_Hello_2.c -o Pth_Hello_2 -lpthread // // Usage: ./Pth_Hello_2 <thread_count> // /////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <pthread.h> const int MAX_THREADS = 64; //Global variable: accessible to all threads int thread_count; void Usage(char* prog_name); void *Hello(void* rank); //Thread function int main(int argc, char* argv[]) { long thread; //Use long in case of a 64-bit system pthread_t* thread_handles; //Get number of threads from command line if (argc != 2) Usage(argv[0]); thread_count = strtol(argv[1], NULL, 10); if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]); thread_handles = malloc (thread_count*sizeof(pthread_t)); for (thread = 0; thread < thread_count; thread++) pthread_create(&thread_handles[thread], NULL, Hello, (void*) thread); printf("Hello from the main thread\n"); for (thread = 0; thread < thread_count; thread++) pthread_join(thread_handles[thread], NULL); free(thread_handles); return 0; } /* main */ void *Hello(void* rank) { long my_rank = (long) rank; //Use long in case of 64-bit system printf("Hello from thread %ld of %d\n", my_rank, thread_count); return NULL; } /* Hello */ void Usage(char* prog_name) { fprintf(stderr, "usage: %s <number of threads>\n", prog_name); fprintf(stderr, "0 < number of threads <= %d\n", MAX_THREADS); exit(0); } /* Usage */