matrix multiplication

Hi,

I'm trying to write what seems like ought to be a fairly straightforward program but it doesn't seem to be giving the right answers. The program is supposed to create two matrices and multiply them.

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(
	}
}


When I use the arguments in main of 1 2 and 3 in linux terminal I get the following typical output:

********************************************

The number of rows in matrix 1: 1
The number of columns in matrix 1: 2
The number of columns in matrix 2: 3


A element: 7525.370157

B element: 4786.565222

sum: 36020675.078263

C element: 36020675.078263

A element: 404.961123

B element: 974.333790

sum: 394567.305946

C element: 37387965.000968

total C element sum: 37387965.000968


A element: 7525.370157

B element: 128.483204

sum: 966883.670808

C element: 966883.670808

A element: 404.961123

B element: 4756.344629

sum: 1926134.663804

C element: 1361450.976755

total C element sum: 1361450.976755


A element: 7525.370157

B element: 3376.348602

sum: 25408273.011687

C element: 25408273.011687

A element: 404.961123

B element: 4500.549619

sum: 1822547.629396

C element: 27334407.675491

total C element sum: 27334407.675491

A =

[ 7525.370157 404.961123 ]

B =

[ 4786.565222 128.483204 3376.348602 ]
[ 974.333790 4756.344629 4500.549619 ]

C =

[ 37387965.000968 1361450.976755 27334407.675491 ]

Hello from thread 0 out of 3

Hello from thread 1 out of 3

Hello from thread 2 out of 3

************************************************

The correct output for C should be:

36415242.38194813 2893018.3313922016 27230820.636946935

Any suggestions?
The problem is in line 97 where the second "column1" should be "column2"
Topic archived. No new replies allowed.