-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparselu-notaskpool.go
406 lines (360 loc) · 10.3 KB
/
sparselu-notaskpool.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
ported from sparselu_single/sparselu.c
assuming FORCE_TIED_TASKS = false
MANUAL_CUTOFF = false
IF_CUTOFF = false
*/
package main
import (
"flag"
"fmt"
"os"
"runtime"
"strconv"
"sync"
)
var matrixSize, submatrixSize int
/* declaring our own matrix type to avoid triple pointers,
this is necessary because go doesn't offer C-style pointer<->array duality,
so accessing the 2d array passed by a pointer would be unnecessarily troublesome
type Matrix [][]float32
*/
/***********************************************************************
* checkmat:
**********************************************************************/
func checkmat(M [][]float32, N [][]float32) bool {
var r_err, EPSILON float32
EPSILON = 1e-6
for i := 0; i < submatrixSize; i++ {
for j := 0; j < submatrixSize; j++ {
r_err = M[i][j] - N[i][j]
if r_err == 0.0 {
continue
}
if r_err < 0.0 {
r_err = -r_err
}
if M[i][j] == 0.0 {
fmt.Printf("Checking failure: A[%d][%d]=%f B[%d][%d]=%f; \n",
i, j, M[i][j], i, j, N[i][j])
return false
}
r_err = r_err / M[i][j]
if r_err > EPSILON {
fmt.Printf("Checking failure: A[%d][%d]=%f B[%d][%d]=%f; Relative Error=%f\n",
i, j, M[i][j], i, j, N[i][j], r_err)
return false
}
}
}
return true
}
/***********************************************************************
* genmat:
**********************************************************************/
func genmat(M []*[][]float32) {
var null_entry bool
init_val := 1325
/* generating the structure */
for ii := 0; ii < matrixSize; ii++ {
for jj := 0; jj < matrixSize; jj++ {
/* computing null entries */
null_entry = false
if (ii < jj) && (ii%3 != 0) {
null_entry = true
}
if (ii > jj) && (jj%3 != 0) {
null_entry = true
}
if ii%2 == 1 {
null_entry = true
}
if jj%2 == 1 {
null_entry = true
}
if ii == jj {
null_entry = false
}
if ii == jj-1 {
null_entry = false
}
if ii-1 == jj {
null_entry = false
}
/* allocating matrix */
if null_entry == false {
// In go, we need to initialize a 2d array by initializing the first dimension and
// then looping over that, initializing the 2nd dimension: https://golang.org/doc/effective_go.html
subMatrix := make([][]float32, submatrixSize)
for i := range subMatrix {
subMatrix[i] = make([]float32, submatrixSize)
}
M[ii*matrixSize+jj] = &subMatrix
/* error checking not really necessary, because unlike malloc(), make() doesn't simply return "nil" on failure.
if ((M[ii*matrixSize+jj] == nil)) {
bots_message("Error: Out of memory\n");
exit(101);
}
*/
/* initializing matrix */
for i := 0; i < submatrixSize; i++ {
for j := 0; j < submatrixSize; j++ {
init_val = (3125 * init_val) % 65536
subMatrix[i][j] = (float32)(init_val-32768.0) / 16384.0
//fmt.Printf("ii=%d\tjj=%d\ti=%d\tj=%d\tsetting content to %.9f\n", ii, jj, i, j, subMatrix[i][j])
}
}
} else {
M[ii*matrixSize+jj] = nil
}
}
}
}
/***********************************************************************
* print_structure:
**********************************************************************/
func print_structure(name string, M []*[][]float32) {
fmt.Printf("Structure for matrix %s @ %p\n", name, M)
for ii := 0; ii < matrixSize; ii++ {
for jj := 0; jj < matrixSize; jj++ {
if M[ii*matrixSize+jj] != nil {
fmt.Print("x")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
fmt.Print("\n")
}
/***********************************************************************
* allocate_clean_block:
**********************************************************************/
/*
func allocate_clean_block() *float32 {
var p, q *float32
p = make(float32, submatrixSize*submatrixSize)
q = p
if p != nil {
for i := 0; i < submatrixSize; i++ {
for j := 0; j < submatrixSize; j++ {
*p = 0.0
p++
}
}
} else {
fmt.Println("Error: Out of memory")
exit(101)
}
return (q)
}
*/
/***********************************************************************
* lu0:
**********************************************************************/
func lu0(diag [][]float32) {
for k := 0; k < submatrixSize; k++ {
for i := k + 1; i < submatrixSize; i++ {
diag[i][k] = diag[i][k] / diag[k][k]
for j := k + 1; j < submatrixSize; j++ {
diag[i][j] = diag[i][j] - diag[i][k]*diag[k][j]
}
}
}
}
/***********************************************************************
* bdiv:
**********************************************************************/
func bdiv(diag [][]float32, row [][]float32) {
for i := 0; i < submatrixSize; i++ {
for k := 0; k < submatrixSize; k++ {
row[i][k] = row[i][k] / diag[k][k]
for j := k + 1; j < submatrixSize; j++ {
row[i][j] = row[i][j] - row[i][k]*diag[k][j]
}
}
}
}
/***********************************************************************
* bmod:
**********************************************************************/
func bmod(row [][]float32, col [][]float32, inner [][]float32) {
for i := 0; i < submatrixSize; i++ {
for j := 0; j < submatrixSize; j++ {
for k := 0; k < submatrixSize; k++ {
inner[i][j] = inner[i][j] - row[i][k]*col[k][j]
}
}
}
}
/***********************************************************************
* fwd:
**********************************************************************/
func fwd(diag [][]float32, col [][]float32) {
for j := 0; j < submatrixSize; j++ {
for k := 0; k < submatrixSize; k++ {
for i := k + 1; i < submatrixSize; i++ {
col[i][j] = col[i][j] - diag[i][k]*col[k][j]
}
}
}
}
func sparselu_init(pBENCH *[]*[][]float32, pass string) {
*pBENCH = make([]*[][]float32, matrixSize*matrixSize)
genmat(*pBENCH)
print_structure(pass, *pBENCH)
}
func sparselu_par_call(BENCH []*[][]float32) {
fmt.Printf("Computing SparseLU Factorization (%dx%d matrix with %dx%d blocks) ",
matrixSize, matrixSize, submatrixSize, submatrixSize)
// #pragma omp parallel
// #pragma omp single nowait
// #pragma omp task untied
var wg sync.WaitGroup
for kk := 0; kk < matrixSize; kk++ {
lu0(*BENCH[kk*matrixSize+kk])
for jj := kk + 1; jj < matrixSize; jj++ {
if BENCH[kk*matrixSize+jj] != nil {
// #pragma omp task untied firstprivate(kk, jj) shared(BENCH)
wg.Add(1)
kk := kk
jj := jj
go func(wg *sync.WaitGroup) {
defer (*wg).Done()
fwd(*BENCH[kk*matrixSize+kk], *BENCH[kk*matrixSize+jj])
}(&wg)
}
}
for ii := kk + 1; ii < matrixSize; ii++ {
if BENCH[ii*matrixSize+kk] != nil {
wg.Add(1)
// #pragma omp task untied firstprivate(kk, ii) shared(BENCH)
kk := kk
ii := ii
go func(wg *sync.WaitGroup) {
defer (*wg).Done()
bdiv(*BENCH[kk*matrixSize+kk], *BENCH[ii*matrixSize+kk])
}(&wg)
}
}
wg.Wait()
// #pragma omp taskwait
for ii := kk + 1; ii < matrixSize; ii++ {
if BENCH[ii*matrixSize+kk] != nil {
for jj := kk + 1; jj < matrixSize; jj++ {
if BENCH[kk*matrixSize+jj] != nil {
//#pragma omp task untied firstprivate(kk, jj, ii) shared(BENCH)
wg.Add(1)
kk := kk
ii := ii
jj := jj
go func(wg *sync.WaitGroup) {
defer (*wg).Done()
if BENCH[ii*matrixSize+jj] == nil {
subMatrix := make([][]float32, submatrixSize)
// go-style initializing 2d matrix in a loop
for i := range subMatrix {
subMatrix[i] = make([]float32, submatrixSize)
}
BENCH[ii*matrixSize+jj] = &subMatrix
}
bmod(*BENCH[ii*matrixSize+kk], *BENCH[kk*matrixSize+jj], *BENCH[ii*matrixSize+jj])
}(&wg)
}
}
wg.Wait()
// #pragma omp taskwait
}
}
}
fmt.Println(" completed!")
}
func sparselu_seq_call(BENCH []*[][]float32) {
for kk := 0; kk < matrixSize; kk++ {
lu0(*BENCH[kk*matrixSize+kk])
for jj := kk + 1; jj < matrixSize; jj++ {
if BENCH[kk*matrixSize+jj] != nil {
fwd(*BENCH[kk*matrixSize+kk], *BENCH[kk*matrixSize+jj])
}
}
for ii := kk + 1; ii < matrixSize; ii++ {
if BENCH[ii*matrixSize+kk] != nil {
bdiv(*BENCH[kk*matrixSize+kk], *BENCH[ii*matrixSize+kk])
}
}
for ii := kk + 1; ii < matrixSize; ii++ {
if BENCH[ii*matrixSize+kk] != nil {
for jj := kk + 1; jj < matrixSize; jj++ {
if BENCH[kk*matrixSize+jj] != nil {
if BENCH[ii*matrixSize+jj] == nil {
subMatrix := make([][]float32, submatrixSize)
// go-style initializing 2d matrix in a loop
for i := range subMatrix {
subMatrix[i] = make([]float32, submatrixSize)
}
BENCH[ii*matrixSize+jj] = &subMatrix
}
bmod(*BENCH[ii*matrixSize+kk], *BENCH[kk*matrixSize+jj], *BENCH[ii*matrixSize+jj])
}
}
}
}
}
}
func sparselu_fini(BENCH []*[][]float32, pass string) {
print_structure(pass, BENCH)
}
func sparselu_check(SEQ []*[][]float32, BENCH []*[][]float32) bool {
var ok = true
for ii := 0; (ii < matrixSize) && ok; ii++ {
for jj := 0; (jj < matrixSize) && ok; jj++ {
if (SEQ[ii*matrixSize+jj] == nil) && (BENCH[ii*matrixSize+jj] != nil) {
ok = false
}
if (SEQ[ii*matrixSize+jj] != nil) && (BENCH[ii*matrixSize+jj] == nil) {
ok = false
}
if (SEQ[ii*matrixSize+jj] != nil) && (BENCH[ii*matrixSize+jj] != nil) {
ok = checkmat(*SEQ[ii*matrixSize+jj], *BENCH[ii*matrixSize+jj])
}
}
}
if ok {
return true
} else {
return false
}
}
func main() {
var start, end float64
bindThreads := os.Getenv("OMP_PROC_BIND")
if bindThreads == "TRUE" {
runtime.LockOSThread()
}
numThreads, err := strconv.Atoi(os.Getenv("OMP_NUM_THREADS"))
if err != nil || numThreads < 1 {
numThreads = runtime.NumCPU()
}
runtime.GOMAXPROCS(numThreads)
flag.IntVar(&matrixSize, "n", 50, "Matrix size")
flag.IntVar(&submatrixSize, "m", 100, "Submatrix size")
flag.Parse()
/*
var matrixSeq []*[][]float32
sparselu_init(&matrixSeq, "Sequential")
sparselu_seq_call(matrixSeq)
*/
//pool.Start()
var matrixPar []*[][]float32
sparselu_init(&matrixPar, "Parallel")
{
// we're not measuring initialization time, just like BOTS
start = Wtime_sec()
sparselu_par_call(matrixPar)
end = Wtime_sec()
}
sparselu_fini(matrixPar, "Parallel")
//pool.Stop()
fmt.Printf("Program time: %.6f s\n", end-start)
// fmt.Println("checking if results seq/par are identical:", sparselu_check(matrixSeq, matrixPar))
}