Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <time.h>
- #ifndef N
- #define N (2)
- #endif
- //using namespace std;
- int A[N][N]={2, 2, 2, 2}, B[N][N]={3,3,3,3}, C[N][N];
- static int COLS;
- static int ROWS;
- static int THREADS;
- typedef struct argMatrix_tag {
- int id;
- int rows;
- int cols;
- int from;
- int to;
- int **a;
- int **b;
- int **c;
- } argMatrix_t;
- void* func(void* num)
- {
- //printf("n = %lx\n", (off_t)num);
- // TODO: matrix * matrix // got it
- for(int i = 0; i < N; i++)
- for(int j = 0; j < N; j++)
- {
- C[i][j] = 0;
- for(int k = 0; k < N; k++)
- C[i][j] += A[i][k] * B[k][j];
- }
- return NULL;
- }
- void* mulrow(void *arg) {
- argMatrix_t *mrx = (argMatrix_t*) arg;
- int i, j, row_index;
- for (row_index = mrx->from; row_index < mrx->to; row_index++) {
- for (i = 0; i < mrx->rows; i++) {
- for (j = 0; j < mrx->cols; j++) {
- mrx->c[row_index][i] += mrx->a[row_index][j] * mrx->b[j][i];
- }
- }
- }
- return 0;
- }
- void createMatrix(int ***a, size_t rows, size_t cols) { //inline
- size_t i;
- (*a) = (int**) malloc(rows * sizeof(int*));
- (*a)[0] = (int*) malloc(rows * cols * sizeof(int));
- for (i = 1; i < rows; i++) {
- (*a)[i] = (*a)[0] + i * cols;
- }
- }
- void freeMatrix(int ***a) {
- free((*a)[0]);
- free(*a);
- *a = NULL;
- }
- int fillRand(size_t i, size_t j) { //inline
- return rand() % 16;
- }
- //inline
- void fillMatrix(int **a, size_t rows, size_t cols, int (*f)(size_t, size_t)) {
- size_t i, j;
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- a[i][j] = f(i, j);
- }
- }
- }
- void testing()
- {
- int **a = NULL;
- int **b = NULL;
- int **c = NULL;
- argMatrix_t *mrx;
- pthread_t *threads;
- int i, j, k, t_tick, s_tick, attempt_counter;
- int rows_per_thread, residue;
- long int frequency, t1, t2;
- double elapsedTime;
- FILE *out = NULL;
- const char *filename = "statistics.txt";
- //float times[ATTEMPTS];
- float mean_time;
- ROWS = COLS = 500; // MRX SIZE
- createMatrix(&a, ROWS, COLS);
- createMatrix(&b, COLS, ROWS);
- createMatrix(&c, COLS, COLS);
- fillMatrix(a, ROWS, COLS, fillRand);
- fillMatrix(b, COLS, ROWS, fillRand);
- // THREADS +++++++++++++++++++++++++++++++++++++++++++++++
- THREADS = 30;
- mrx = (argMatrix_t*) malloc(sizeof(argMatrix_t)*THREADS);
- threads = (pthread_t*) malloc(sizeof(pthread_t)*THREADS);
- rows_per_thread = ROWS / THREADS;
- residue = ROWS % THREADS;
- for (i = 0; i < THREADS; i++)
- {
- mrx[i].id = i;
- mrx[i].a = a;
- mrx[i].b = b;
- mrx[i].c = c;
- mrx[i].from = rows_per_thread * i;
- mrx[i].to = (i + 1)*rows_per_thread;
- mrx[i].cols = COLS;
- mrx[i].rows = ROWS;
- }
- mrx[THREADS - 1].to += residue;
- for (i = 0; i < THREADS; i++)
- {
- pthread_create(&threads[i], NULL, mulrow, (void*)&mrx[i]);
- }
- for (i = 0; i < THREADS; i++)
- {
- pthread_join(threads[i], NULL);
- }
- free(threads);
- free(mrx);
- freeMatrix(&a);
- freeMatrix(&b);
- freeMatrix(&c);
- }
- int main (int argc, char* argv[])
- {
- /* pthread_t pid;
- void *res;
- pthread_create(&pid, NULL, func, (void*)1);
- pthread_join(pid, &res);
- // TODO display matrix // got it
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- printf("%d ", C[i][j]);
- }
- }
- */
- //time_t start, end;
- //time(&start);
- float ttt = clock();
- testing();
- //for(int k=0; k<99999999; k++);
- printf("THREADS = %d", THREADS);
- printf("\n");
- float answer = ((float)ttt) / CLOCKS_PER_SEC;
- printf("%.30f", answer);
- printf("\n");
- //time(&end);
- //double seconds = difftime(end, start);
- //printf("The time: %f seconds\n", seconds);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement