/
matrix.xc
227 lines (218 loc) · 5.07 KB
/
matrix.xc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Matrix manipulation operations
*
* Copyright (C) 2011 Steve Kerrison <github@stevekerrison.com>
*
* This software is freely distributable under a derivative of the
* University of Illinois/NCSA Open Source License posted in
* LICENSE.txt and at <http://github.xcore.com/>
*/
#include <stdio.h> //For matrix_print
#include "matrix.h"
#include "matrix_worker.h"
#include "xs1.h"
{int,int} matrix_calc_block(int size, int nthreads)
{
int blockSize, blockRem, lastBlock;
blockSize = size / nthreads;
blockRem = size % blockSize;
if (blockRem != 0)
{
if ((blockSize + 1)*(nthreads-1) < size)
{
blockSize += 1;
}
lastBlock = size - (blockSize * (nthreads-1));
}
else
{
lastBlock = blockSize;
}
return {blockSize,lastBlock};
}
int matrix_redim(short dims[4],short rows, short columns)
{
if (dims[2] < rows || dims[3] < columns)
{
return -1; //Larger than initial size
}
dims[0] = rows;
dims[1] = columns;
return rows * columns;
}
int matrix_sca_op(enum matrix_ops op, int A[], short dimA[2], int S,
int ? C[], short ? dimC[2], char nThreads)
{
int retval[8] = {0,0,0,0,0,0,0,0}, i;
int ptA, ptC, ptDimA, ptRetval;
int srcSize = dimA[0] * dimA[1], blockSize, lastBlock;
POINTER(ptA,A);
POINTER(ptC,C);
POINTER(ptDimA,dimA);
POINTER(ptRetval,retval);
/* First do some sanity checks... */
if (isnull(C))
{
//No checks yet
}
else
{
if (dimC[1] < dimA[1])
{
return -4; //Not enough columns in destination matrix
}
if (dimC[0] < dimA[0])
{
return -5; //Not enough rows in destination matrix
}
}
if (isnull(C))
{
ptC = ptA;
}
/* Early-out for small data */
if (srcSize < MATRIX_NTHREADS * MATRIX_NTHREADS)
{
matrix_sca_worker(op,ptA,S,ptC,ptRetval,
0, srcSize);
return retval[0];
}
/* More optimal distribution of workload */
{blockSize,lastBlock} = matrix_calc_block(srcSize,MATRIX_NTHREADS);
par
{
par (int t = 0; t < MATRIX_NTHREADS-1; t++)
{
matrix_sca_worker(op,ptA,S,ptC,ptRetval+(t * sizeof(int)),
blockSize * t, blockSize);
}
matrix_sca_worker(op,ptA,S,ptC,ptRetval+((MATRIX_NTHREADS-1) * sizeof(int)),
blockSize * (MATRIX_NTHREADS-1), lastBlock);
}
for (i = 1; i < 8; i++)
{
retval[0] += retval[i];
}
return retval[0];
}
int matrix_arr_op(enum matrix_ops op, int A[], short dimA[2], int B[], short dimB[2],
int ? C[], short ? dimC[2], char nThreads)
{
int retval[8] = {0,0,0,0,0,0,0,0}, i;
int ptA, ptB, ptC, ptDimA, ptDimB, ptRetval;
int srcSize = dimA[0] * dimA[1], blockSize, lastBlock;
POINTER(ptA,A);
POINTER(ptB,B);
POINTER(ptC,C);
POINTER(ptDimA,dimA);
POINTER(ptDimB,dimB);
POINTER(ptRetval,retval);
/*int ptA = pointer_int(A), ptB = pointer_int(B), ptC = pointer_int(C),
ptDimA = pointer_short(dimA), ptDimB = pointer_short(dimB),
ptRetval = pointer_int(retval);*/
/* First do some sanity checks... */
if (dimA[0] != dimB[0] || dimA[1] != dimB[1]) return -2; //Invalid dimensions
if (isnull(C))
{
//No checks yet
}
else
{
if (dimC[1] < dimB[1])
{
return -4; //Not enough columns in destination matrix
}
if (dimC[0] < dimA[0])
{
return -5; //Not enough rows in destination matrix
}
}
if (isnull(C))
{
ptC = ptA;
}
/* Early-out for small data */
if (srcSize < MATRIX_NTHREADS * MATRIX_NTHREADS)
{
matrix_arr_worker(op,ptA,ptB,ptC,ptRetval,
0, srcSize);
return retval[0];
}
/* More optimal distribution of workload */
{blockSize,lastBlock} = matrix_calc_block(srcSize,MATRIX_NTHREADS);
par
{
par (int t = 0; t < MATRIX_NTHREADS-1; t++)
{
matrix_arr_worker(op,ptA,ptB,ptC,ptRetval+(t * sizeof(int)),
blockSize * t, blockSize);
}
matrix_arr_worker(op,ptA,ptB,ptC,ptRetval+((MATRIX_NTHREADS-1) * sizeof(int)),
blockSize * (MATRIX_NTHREADS-1), lastBlock);
}
for (i = 1; i < 8; i++)
{
retval[0] += retval[i];
}
return retval[0];
}
int matrix_mul(int A[], short dimA[2], int B[], short dimB[2],
int ? C[], short ? dimC[2], char nThreads)
{
int retval[8] = {0,0,0,0,0,0,0,0}, i;
int ptA, ptB, ptC, ptDimA, ptDimB, ptRetval;
POINTER(ptA,A);
POINTER(ptB,B);
POINTER(ptC,C);
POINTER(ptDimA,dimA);
POINTER(ptDimB,dimB);
POINTER(ptRetval,retval);
/* First do some sanity checks... */
if (dimA[1] != dimB[0]) return -2; //Matrices cannot be multiplied
if (isnull(C))
{
if (dimA[1] < dimB[1])
return -3; //Inline result cannot fit in matrix A.
}
else
{
if (dimC[1] < dimB[1])
{
return -4; //Not enough columns in destination matrix
}
if (dimC[0] < dimA[0])
{
return -5; //Not enough rows in destination matrix
}
}
if (isnull(C))
{
//FIXME - Use a thread-safe strategy for in-place results
return -1; //In-place result not supported at the moment
}
par (int t = 0; t < MATRIX_NTHREADS; t++)
{
matrix_mul_worker(ptA,ptDimA,ptB,ptDimB,ptC,ptRetval,
MATRIX_NTHREADS, t);
}
for (i = 1; i < 8; i++)
{
retval[0] += retval[i];
}
return retval[0];
}
void matrix_print(char name[], int M[], short dimM[2])
{
int r,c,s = dimM[0] * dimM[1];
printf("Matrix %s =\n", name);
for (r = 0; r < s; r += dimM[1])
{
printf(" ");
for (c = 0; c < dimM[1]; c++)
{
printf(" %d ",M[r + c]);
}
printf("\n");
}
printf("\n");
}