Created
May 12, 2013 23:01
-
-
Save gceylan/5565283 to your computer and use it in GitHub Desktop.
multithread matrix multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#define MATRIX_SIZE 16 | |
#define RANDOM_LENGHT 10000 | |
#define NTHREAD 4 | |
#define ROW_LENGHT (MATRIX_SIZE/NTHREAD) | |
static int **matrix_a; | |
static int **matrix_b; | |
static int **matrix_mult; | |
static int **matrix_memory_allocation() { | |
int i; | |
int j; | |
int *vector; | |
int **matrix; | |
i = 0; | |
j = 0; | |
/* vektör */ | |
vector = (int *) malloc(MATRIX_SIZE * MATRIX_SIZE * sizeof(int *)); | |
matrix = (int **) malloc(MATRIX_SIZE * sizeof(int *)); | |
for (i = 0; i < MATRIX_SIZE; i++) { | |
matrix[i] = &(vector[i * MATRIX_SIZE]); | |
} | |
srand(time(NULL)); | |
for (i = 0; i < MATRIX_SIZE; i++) { | |
for (j = 0; j < MATRIX_SIZE; j++) { | |
matrix[i][j] = random() % RANDOM_LENGHT; | |
} | |
} | |
return matrix; | |
} | |
static void print_matrix(int **m) | |
{ | |
int i; | |
int j; | |
for (i = 0; i < MATRIX_SIZE; i++) { | |
for (j = 0; j < MATRIX_SIZE; j++) { | |
printf("%-15d", m[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
static void init(void) | |
{ | |
matrix_a = matrix_memory_allocation(); | |
matrix_b = matrix_memory_allocation(); | |
matrix_mult = matrix_memory_allocation(); | |
printf("matrix_a:\n"); | |
print_matrix(matrix_a); | |
printf("matrix_b:\n"); | |
print_matrix(matrix_b); | |
} | |
/* | |
* arg -> threads[i] | |
* | |
* */ | |
static void *process(void *arg) | |
{ | |
uintptr_t offset; | |
long i, j, k, start, end; | |
long sum; | |
/* | |
* buradaki tip dönüşümü önemli | |
* programcı bu tip dönüşümünü bilinçli yapmalıdır | |
* | |
* */ | |
offset = (uintptr_t) arg; | |
start = offset * ROW_LENGHT ; | |
end = start + ROW_LENGHT; | |
for (i = start; i < end; i++) { | |
for (j = 0; j < MATRIX_SIZE; j++) { | |
sum = 0; | |
for (k = 0; k < MATRIX_SIZE; k++) { | |
sum += matrix_a[i][k] * matrix_b[k][j]; | |
} | |
matrix_mult[i][j] = sum; | |
} | |
} | |
pthread_exit(NULL); | |
} | |
static void report() | |
{ | |
printf("matrix_a * matrix_b:\n"); | |
print_matrix(matrix_mult); | |
} | |
static void shutdown(void) | |
{ | |
free(matrix_a); | |
free(matrix_b); | |
free(matrix_mult); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
uintptr_t i; | |
pthread_t threads[NTHREAD]; | |
init(); | |
for (i = 0; i < NTHREAD; i++) { | |
if (pthread_create(&threads[i], NULL, process, (void *) i)) { | |
fprintf(stderr, "Cannot create thread!\n"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
for (i = 0; i < NTHREAD; i++) | |
pthread_join(threads[i], NULL); | |
report(); | |
shutdown(); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment