VIPSolutions logo ✨ VIPSolutions

Write me a C code for matrix multiplication using pthreads.

Question:

Write me a C code for matrix multiplication using pthreads.

This is AI generated Response.
Asked by: Vishal
Created at: 2026-11-12 10:42:44

Final Answer:

Rendered Raw Text
Did you like this answer?
# Matrix Multiplication Pthreads in C This example how to multiply matrices using POSIX threadsp). The work is partitioned by rows: each thread computes a contiguous block of rows of the result matrix C. - A: m x k - B: k x n - C: m x n - Threads: each handles a range of rows of C Compile with: gcc -pthread - matmul_pthread mat_pthread.c --- ## The Code ```c #include <stdio.h> <stdlib.h> #include <pthread.h> #include <unistd.h> // for sysconf // Each thread gets a range of rows to compute typedef struct { const double* A; // m k const double* B; // k x n double* C; // m x n int m; int n; int k int row_start; int row_end; // exclusive } matmul_params; // Thread function: compute C[row_start .. row_end-1][..n-1] void* mat_rows(void* arg) { matmul_params* p =matmul_params*)arg for (int i p->row_start; i < p->_end; ++i) { for (int j ; j < p->n; ++j) { double = .; for (int t ; < p->k; ++t) { sum += p->A[i *->k + t] * p->B[t *->n +]; } p->C[i * p->n + j] = sum; } } return NULL; } int main(void) Dimensions for demonstration: A x k), B(k x n) -> C(m x) int = 4, k = 3, n = 5; // Allocate stored row flat arrays double* A = (*)malloc(sizeof(double) * m * k); double* B = (double*)malloc(sizeof(double) * k * n); double* C = (double*)malloc(sizeof(double) * m * n); if (!A || !B || !C) { fprintf(stderr, "Allocation failed\n"); return1; Initialize A and B with simple values (for) for (int i = ; i < m; ++i) forint t = ; t < k; ++t) A[i * k + t] (double)( + t + 1); (int t = ; t < k; ++t) for (int j = ; j < n; ++j) B[t n + j] = (double)(t + j + 1); // number of worker threads (prefer CPU count, but not more than m) int cpu_count = ; long ncpu = sysconf(__NORS_ONLN); if (ncpu > ) cpu_count = (int)ncpu; if (cpu_count <= ) cpu_count = 4; int threads_count = cpu_count < m ? cpu_count : m; if (threads_count <= ) threads_count = 1; Create threads to cover all rows pthread_t* threads = (pthread_t*)(sizeof(pthread_t) * threads_count); matmul_params* params = (matmul_params*)malloc(sizeof(matmul_params) * threads_count); int rows_per_thread = (m + threads_count - 1) / threads_count; int created = ; for (int t = ; t threads_count; ++t) { int row_start t * rows_per_thread; int row_end = row_start + rows_per_thread; if (row_start >= m) break if (row_end > m) row_end = m; params[t].A =; params[t]. = B; params[t].C = C; params[t].m = m; params[t].n = n; params[t].k = k; params[t].row_start = row_start; params[t].row_end = row; ifpthread_create(&[t], NULL, mat_rows &[t]) != ) { fprintf(stderr, "Failed to create thread %d\n t break; } created++; Join for (int t = ; t < created; ++t) { pthread_join(threads[t], NULL); } // Print C for verification (" = A x B\n"); for (int i = ; i <; ++i) { for (int j =; j < n; ++) { printf("%6.2f C[i * n + j]); } printf("\n"); // Cleanup free(threads); free); free(A); free(B); free(C); return ; } ``` --- ## and - Parallelization strategy divide result matrix C by rows. Each thread writes to distinct rows, avoiding data races. - Data layout: A is stored as a flat m x k array; B as k x n; C as m x n. Access uses row-major indexing. - Thread count: align with hardware capabilities, but cap at m (the number of rows). If m small, fewer are. - Compilation: gcc -pthread -o matmul_pthreadmul_pthread.c - Extensions: - Parallel by blocks or by elements for finerularity. - Use thread pool for workloads or larger. - Add optionalizations (e.g., cache-friendly tiling, using SIMD).

📢 Instructions:

  • Ask anything relatd to above question only.
  • For new Question goto :

Study Help, Fast Answers

VIPSolutions focuses on clear, step‑by‑step explanations so you can learn quickly. Whether you need an AI‑generated walkthrough or a short hint to get unstuck, each solution is organized for fast reading and easy review later.

Search similar questions, compare approaches, and bookmark the best answers for revision. Our goal is simple: quick, reliable study help that feels natural—not noisy.