1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
void initialize_data(double *A, short rows, short columns);//create N * N matrix containing random integers
void print_array(double *A, short rows, short columns);
void matrix_mult(double *A, short rows1, short columns1, double *B, short columns2, double *C);// A * B = C matrix multiplication (non-commutative)
//void print_matrix(short matrix, short rows, short columns);//prints matrix of size rows * columns
int main(int argc, char *argv[]) {
short tid;
short np;
srand ( time(NULL) );
short rows1 = atof(argv[1]);
short columns1 = atof(argv[2]);
short rows2 = columns1;
short columns2 = atof(argv[3]);
printf ("\nThe number of rows in matrix 1: %d\nThe number of columns in matrix 1: %d\nThe number of columns in matrix 2: %d\n\n", rows1, columns1, columns2);
double *A = malloc(sizeof(double) * ( rows1 * columns1));
double *B = malloc(sizeof(double) * ( rows2 * columns2));
initialize_data( A, rows1, columns1);
initialize_data( B, rows2, columns2);
//initialize_data( C, rows1, columns2);
double *C = malloc(sizeof(double) * (2 * 3));
//double *C = malloc(sizeof(double) * (rows1 * columns2));
matrix_mult( A, rows1, columns1, B, columns2, C);
//initialize_data( C, 2, 3);
printf ("A =\n\n");
print_array (A, rows1, columns1);
printf ("\nB =\n\n");
print_array (B, rows2, columns2);
printf ("\nC =\n\n");
print_array (C, rows1, columns2);
#pragma omp parallel default(shared) private(tid, np)
{
np = omp_get_num_threads();
tid = omp_get_thread_num();
printf ("\nHello from thread %d out of %d\n", tid, np-1);
}
return 0;
}
void initialize_data(double *A, short rows, short columns)
{
time_t t;
//srand ( time(NULL) );
short i;
short j;
float upper_bound = 10000;
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
A[i * columns + j ] = (((double)rand()) / ((double)RAND_MAX)) * upper_bound;
}
}
}
void print_array(double *A, short rows, short columns)
{
short i = 0;
short j = 0;
for(i = 0; i < rows; i++)
{
printf("[ ");
for(j = 0; j < columns; j++)
{
printf(" %05f ", A[i * columns + j]);
}
printf(" ]\n");
}
}
void matrix_mult(double *A, short rows1, short columns1, double *B, short columns2, double *C)
{
short i, j;
for (i = 0; i < rows1; i ++)
{
for (j = 0; j < columns2; j++)
{
C[i * columns2 + j] = 0;
short k;
for(k = 0; k < columns1; k++)
{
printf ("\nA element: %f\n", A[i * columns1 + k]);
printf ("\nB element: %f\n", B[k * columns2/*rows2*/ + j]);
C[i * columns2 + j] += (A[i * columns1 + k] * B[k * columns1/*rows2*/ + j]);
printf ("\nsum: %f\n", A[i * columns1 + k] * B[k * columns2/*rows2*/ + j]);
printf ("\nC element: %f\n", C[i * columns2 + j]);
}
printf ("\ntotal C element sum: %f\n\n", C[i * columns2 + j]);
}
//print_array(
}
}
|