-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcg.cpp
More file actions
1854 lines (1610 loc) · 61 KB
/
cg.cpp
File metadata and controls
1854 lines (1610 loc) · 61 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <setjmp.h>
#include <stdbool.h>
#include <signal.h>
#include <vector>
#include <mammut/mammut.hpp>
#include "mammut_functions.hpp"
#include "timers.h"
#include "mpi.h"
#include "mpi-ext.h"
//#define DEBUG_ITER_COUNT
//#define ENABLE_EVENT_LOG_ 0
#ifdef ENABLE_EVENT_LOG_
int EVENT_LOGGER;
#endif
#include <math.h>
#define max(a,b) ((a>b) ? (a) : (b))
#define min(a,b) ((a<b) ? (a) : (b))
static double start_it;
static int max_iter = 0;
static bool post_failure = false;
static bool post_failure_sync = false;
static int FAILED_PROC;
static int KILL_OUTER_ITER;
static int KILL_INNER_ITER;
static int KILL_PHASE;
#define CGITMAX 100
// from S class
//#define NONZER 7
#define NITER 2
//#define SHIFT 10.0
#define RCOND 1.0e-1
//#define NA 1400
//
//from A clas
//
//#define NA 14000
//#define NONZER 11
//#define SHIFT 20
//from B class
//#define NA 75000
//#define NONZER 13
//#define SHIFT 60
//#define NITER 25
//#define RCOND 1.0e-1
//from C class
//#define NA 150000
//#define NITER 75
//#define NONZER 15
//#define SHIFT 110
// from D class
#define NA 1500000
#define NONZER 21
#define SHIFT 500
//E class
//#define NA 9000000
//#define NONZER 26
//#define SHIFT 1500
#define NZ NA*(NONZER+1)*(NONZER+1)+NA*(NONZER+2)+NONZER
/* common /partit_size/ */
typedef int boolean;
static int naa, nzz, firstrow, lastrow, firstcol, lastcol, send_start, send_len;
bool ap_q, exchange, obtain_sum, obtain_rho;
//static double w[NA+2+1]; /* w[1:NA+2] */
//
///* common /urando/ */
static int dummy, dummy2;
static int allowed_to_kill = 1;
static int killed_times = 0;
static int *last_ckpt;
static int cgit;
static int * peer_iters;
static double ** replay1;
static double *** replay2;
static double ** replay4;
static double * replay5;
static int * replay_ind = 0;
static int outer_iter;
static double amult;
static double tran;
int l2npcols, nprocs, npcols, nprows, exch_recv_length, exch_proc;
int me;
extern double randlc(double *, double);
/* function declarations */
static void conj_grad (int colidx[], int rowstr[], double x[], double z[], double a[], double p[], double q[], double r[], double w[], double *rnorm, int reduce_exch_proc[], int reduce_send_starts[], int reduce_send_lengths[], int reduce_recv_starts[], int reduce_recv_lengths[], MPI_Comm * parent);
static void makea(int n, int nz, double a[], int colidx[], int rowstr[], int nonzer, int firstrow, int lastrow, int firstcol, int lastcol, double rcond, int arow[], int acol[], double aelt[], double v[], int iv[], double shift );
static void sparse(double a[], int colidx[], int rowstr[], int n, int arow[], int acol[], double aelt[], int firstrow, int lastrow, double x[], boolean mark[], int nzloc[], int nnza);
static void sprnvc(int n, int nz, double v[], int iv[], int nzloc[], int mark[]);
static int icnvrt(double x, int ipwr2);
static void vecset(int n, double v[], int iv[], int *nzv, int i, double val);
static int MPIX_Comm_replace(MPI_Comm comm, MPI_Comm *newcomm);
int verbose = 1;
//static int rank = MPI_PROC_NULL, verbose = 1; /* makes this global (for printfs) */
static char estr[MPI_MAX_ERROR_STRING]=""; static int strl; /* error messages */
char** gargv;
static int iteration = 0, ckpt_iteration = 0, last_dead = MPI_PROC_NULL;
static jmp_buf stack_jmp_buf;
/* mockup checkpoint restart: we reset iteration, and we prevent further
* error injection */
static int app_reload_ckpt(MPI_Comm comm)
{
/* Fall back to the last checkpoint */
MPI_Allreduce(&ckpt_iteration, &iteration, 1, MPI_INT, MPI_MAX, comm);
iteration++;
return 0;
}
/* world will swap between worldc[0] and worldc[1] after each respawn */
static MPI_Comm worldc[2] = { MPI_COMM_NULL, MPI_COMM_NULL };
static int worldi = 0;
#define world (worldc[worldi])
void setup_failures() {
FILE * f = fopen("failures.cg.conf", "r");
if (f == NULL) {
fprintf(stderr, "No failure config file\n");
MPI_Abort(world, -1);
}
fscanf (f, "%d : %d : %d : %d", &FAILED_PROC, &KILL_OUTER_ITER, &KILL_INNER_ITER, &KILL_PHASE);
fclose(f);
}
/* repair comm world, reload checkpoints, etc...
* Return: true: the app needs to redo some iterations
* false: no failure was fixed, we do not need to redo any work.
*/
static int app_needs_repair(MPI_Comm comm)
{
printf("(Rank %d): enter app_needs_repair\n", me);
/* This is the first time we see an error on this comm, do the swap of the
* worlds. Next time we will have nothing to do. */
if( comm == world ) {
/* swap the worlds */
worldi = (worldi+1)%2;
/* We keep comm around so that the error handler remains attached until the
* user has completed all pending ops; it is expected that the user will
* complete all ops on comm before posting new ops in the new world.
* Beware that if the user does not complete all ops on comm and the handler
* is invoked on the new world inbetween, comm may be freed while
* operations are still pending on it, and a fatal error may be
* triggered when these ops are finally completed (possibly in Finalize)*/
if( MPI_COMM_NULL != world ) MPI_Comm_free(&world);
MPIX_Comm_replace(comm, &world);
// ToDo: shouldn't have to do that here?
//app_reload_ckpt(world);
if( MPI_COMM_NULL == comm ) return false; /* ok, we repaired nothing, no need to redo any work */
//do_recover = 1;
//goto restart;
longjmp( stack_jmp_buf, 1 );
}
return true; /* we have repaired the world, we need to reexecute */
}
/* Do all the magic in the error handler */
static void errhandler_respawn(MPI_Comm* pcomm, int* errcode, ...)
{
//int rank; MPI_Comm_rank(*pcomm, &rank);
//printf("DEBUG: rank in respawn -> %d\n", rank);
int eclass;
MPI_Error_class(*errcode, &eclass);
//printf("Rank %d: ap_q = %d, exchange = %d, obtain_sum = %d, obtain_rho = %d\n", me, ap_q, exchange, obtain_sum, obtain_rho);
if( verbose ) {
MPI_Error_string(*errcode, estr, &strl);
fprintf(stderr, "%04d: errhandler invoked in cgit = %d with error %s\n", me, cgit, estr);
}
if( MPIX_ERR_PROC_FAILED != eclass &&
MPIX_ERR_REVOKED != eclass ) {
MPI_Abort(MPI_COMM_WORLD, *errcode);
}
MPIX_Comm_revoke(world);
timer_start(2);
app_needs_repair(world);
timer_stop(2);
}
static int MPIX_Comm_replace(MPI_Comm comm, MPI_Comm *newcomm)
{
MPI_Comm icomm, /* the intercomm between the spawnees and the old (shrinked) world */
scomm, /* the local comm for each sides of icomm */
mcomm; /* the intracomm, merged from icomm */
MPI_Group cgrp, sgrp, dgrp;
int rc, flag, rflag, i, crank, srank, drank, nc, ns, nd;
printf("process %d enters replace ...\n", me);
redo:
if( comm == MPI_COMM_NULL ) { /* am I a new process? */
/* I am a new spawnee, waiting for my new rank assignment
* it will be sent by rank 0 in the old world */
MPI_Comm_get_parent(&icomm);
scomm = MPI_COMM_WORLD;
MPI_Recv(&crank, 1, MPI_INT, 0, 1, icomm, MPI_STATUS_IGNORE);
printf("New process should be assigned rank %d\n", crank);
if( verbose ) {
MPI_Comm_rank(scomm, &srank);
printf("Spawnee %d: crank=%d\n", srank, crank);
}
} else {
/* I am a survivor: Spawn the appropriate number
* of replacement processes (we check that this operation worked
* before we procees further) */
/* First: remove dead processes */
MPIX_Comm_shrink(comm, &scomm);
MPI_Comm_size(scomm, &ns);
MPI_Comm_size(comm, &nc);
nd = nc-ns; /* number of deads */
if( 0 == nd ) {
/* Nobody was dead to start with. We are done here */
MPI_Comm_free(&scomm);
*newcomm = comm;
return MPI_SUCCESS;
}
/* We handle failures during this function ourselves... */
MPI_Comm_set_errhandler( scomm, MPI_ERRORS_RETURN );
MPI_Info info;
MPI_Info_create(&info);
MPI_Info_set(info, "host", "m1");
rc = MPI_Comm_spawn(gargv[0], &gargv[1], nd, info,
0, scomm, &icomm, MPI_ERRCODES_IGNORE);
flag = (MPI_SUCCESS == rc);
MPIX_Comm_agree(scomm, &flag);
if( !flag ) {
if( MPI_SUCCESS == rc ) {
MPIX_Comm_revoke(icomm);
MPI_Comm_free(&icomm);
}
MPI_Comm_free(&scomm);
if( verbose ) fprintf(stderr, "%04d: comm_spawn failed, redo\n", me);
goto redo;
}
/* remembering the former rank: we will reassign the same
* ranks in the new world. */
MPI_Comm_rank(comm, &crank);
MPI_Comm_rank(scomm, &srank);
/* the rank 0 in the scomm comm is going to determine the
* ranks at which the spares need to be inserted. */
if(0 == srank) {
/* getting the group of dead processes:
* those in comm, but not in scomm are the deads */
MPI_Comm_group(comm, &cgrp);
MPI_Comm_group(scomm, &sgrp);
MPI_Group_difference(cgrp, sgrp, &dgrp);
/* Computing the rank assignment for the newly inserted spares */
for(i=0; i<nd; i++) {
MPI_Group_translate_ranks(dgrp, 1, &i, cgrp, &drank);
/* sending their new assignment to all new procs */
MPI_Send(&drank, 1, MPI_INT, i, 1, icomm);
// left border (1 failed)
last_dead = drank;
}
MPI_Group_free(&cgrp); MPI_Group_free(&sgrp); MPI_Group_free(&dgrp);
}
}
/* Merge the intercomm, to reconstruct an intracomm (we check
* that this operation worked before we proceed further) */
rc = MPI_Intercomm_merge(icomm, 1, &mcomm);
rflag = flag = (MPI_SUCCESS==rc);
MPIX_Comm_agree(scomm, &flag);
if( MPI_COMM_WORLD != scomm ) MPI_Comm_free(&scomm);
MPIX_Comm_agree(icomm, &rflag);
MPI_Comm_free(&icomm);
if( !(flag && rflag) ) {
if( MPI_SUCCESS == rc ) {
MPI_Comm_free(&mcomm);
}
if( verbose ) fprintf(stderr, "%04d: Intercomm_merge failed, redo\n", me);
goto redo;
}
/* Now, reorder mcomm according to original rank ordering in comm
* Split does the magic: removing spare processes and reordering ranks
* so that all surviving processes remain at their former place */
//printf("Trying to assign rank %d in split\n", crank);
rc = MPI_Comm_split(mcomm, 1, crank, newcomm);
/* Split or some of the communications above may have failed if
* new failures have disrupted the process: we need to
* make sure we succeeded at all ranks, or retry until it works. */
flag = (MPI_SUCCESS==rc);
MPIX_Comm_agree(mcomm, &flag);
MPI_Comm_free(&mcomm);
if( !flag ) {
if( MPI_SUCCESS == rc ) {
MPI_Comm_free( newcomm );
}
if( verbose ) fprintf(stderr, "%04d: comm_split failed, redo\n", me);
goto redo;
}
/* restore the error handler */
if( MPI_COMM_NULL != comm ) {
MPI_Errhandler errh;
MPI_Comm_get_errhandler( comm, &errh );
MPI_Comm_set_errhandler( *newcomm, errh );
}
//printf("Done with the recovery (rank %d)\n", crank);
return MPI_SUCCESS;
}
void FPrintSparseMatrix (FILE *f, int *vptr, int *vpos, double *vval, int dim1)
{
int i, j;
if (vptr == vpos)
{
fprintf (f, "Diagonals : \n ");
for (i=1; i<=dim1; i++) fprintf (f, "%f ", vval[i]); fprintf (f, "\n");
}
fprintf (f, "Pointers: \n ");
if (dim1 > 0)
for (i=1; i<=dim1+1; i++) fprintf (f, "%d ", vptr[i]); fprintf (f, "\n");
fprintf (f, "Values: \n");
for (i=1; i<=dim1; i++) {
fprintf (f, " Row %d --> ", i);
for (j=(vptr[i]); j<(vptr[i+1]); j++)
fprintf (f, "(%d,%lf) ", vpos[j+1], vval[j+1]);
fprintf (f, "\n");
}
fprintf (f, "\n");
}
void FReadSparseMatrix (FILE *f, int *vptr, int *vpos, double *vval, int dim1)
{
int i, j;
if (vptr == vpos)
{
fscanf (f, "Diagonals : \n ");
for (i=1; i<=dim1; i++) fscanf (f, "%f ", &vval[i]); fscanf (f, "\n");
}
fscanf (f, "Pointers: \n ");
if (dim1 > 0)
for (i=1; i<=dim1+1; i++) fscanf (f, "%d ", &vptr[i]); fscanf (f, "\n");
fscanf (f, "Values: \n");
for (i=1; i<=dim1; i++)
{ int k;
fscanf (f, " Row %d --> ", &k);
for (j=(vptr[i]); j<(vptr[i+1]); j++) {
fscanf (f, "(%d,%lf) ", &vpos[j+1], &vval[j+1]);
}
fscanf (f, "\n");
}
fscanf (f, "\n");
}
void writevec(double z[], double p[], double r[], double q[], double rho) {
//don't measure I/O
timer_start(1);
char filename[16];
int dim1 = lastcol-firstcol+1;
sprintf (filename, "/tmp/x-%d.dat", me);
FILE * file = fopen(filename, "w");
if (file == NULL) {
printf("Can't write backup file\n");
MPI_Abort(world, -1);
}
fprintf (file, "(%d,%d,%12.16e)\n", outer_iter, cgit, rho);
for (int i=1; i<=dim1; i++)
fprintf (file, "(%12.16e,%12.16e,%12.16e,%12.16e) ", z[i], p[i], r[i], q[i]);
fprintf (file, "\n");
fclose(file);
timer_stop(1);
}
void readvec (double z[], double p[], double r[], double q[], double *rho) {
//don't measure I/O
timer_start(1);
char filename[16];
int dim1 = lastcol-firstcol+1;
sprintf (filename, "/tmp/x-%d.dat", me);
FILE * file = fopen(filename, "r");
fscanf (file, "(%d,%d,%lf)\n", &outer_iter, &cgit, rho);
//printf("Just read rho = %12.12e\n", rho);
for (int i=1; i<=dim1; i++) {
fscanf (file, "(%lf,%lf,%lf,%lf) ", &z[i], &p[i], &r[i], &q[i]);
//printf("after READ x[%d] = %lf\n", colidx[i], x[colidx[i]]);
}
fscanf (file, "\n");
fclose(file);
timer_stop(1);
}
void writea(int rowstr[], int colidx[], double a[]) {
char filename[16];
sprintf (filename, "a-%d.dat", me);
FILE * file = fopen(filename, "w");
FPrintSparseMatrix(file, rowstr, colidx, a, lastcol-firstcol+1);
// dummy test for Fotran style in C data structures
// double a1[] = {0, 5,8,3,6};
// int rowstr1[] = {0,0,0,2,3,4};
// int colidx1[] = {0,1,2,3,2};
// FPrintSparseMatrix(file, rowstr1, colidx1, a1, 4);
fclose(file);
}
void reada(int rowstr[], int colidx[], double a[]) {
char filename[16];
sprintf (filename, "/tmp/matrix-%d.dat", me);
FILE * file = fopen(filename, "r");
if (file == NULL) {
printf("Can't read matrix file\n");
MPI_Abort(world, -1);
}
FReadSparseMatrix(file, rowstr, colidx, a, lastcol-firstcol+1);
fclose(file);
}
/*---------------------------------------------------------------------
c generate the test problem for benchmark 6
c makea generates a sparse matrix with a
c prescribed sparsity distribution
c
c parameter type usage
c
c input
c
c n i number of cols/rows of matrix
c nz i nonzeros as declared array size
c rcond r*8 condition number
c shift r*8 main diagonal shift
c
c output
c
c a r*8 array for nonzeros
c colidx i col indices
c rowstr i row pointers
c
c workspace
c
c iv, arow, acol i
c v, aelt r*8
c---------------------------------------------------------------------*/
static void makea(
int n,
int nz,
double a[], /* a[1:nz] */
int colidx[], /* colidx[1:nz] */
int rowstr[], /* rowstr[1:n+1] */
int nonzer,
int firstrow,
int lastrow,
int firstcol,
int lastcol,
double rcond,
int arow[], /* arow[1:nz] */
int acol[], /* acol[1:nz] */
double aelt[], /* aelt[1:nz] */
double v[], /* v[1:n+1] */
int iv[], /* iv[1:2*n+1] */
double shift )
{
int i, nnza, iouter, ivelt, ivelt1, irow, nzv;
/*--------------------------------------------------------------------
c nonzer is approximately (int(sqrt(nnza /n)));
c-------------------------------------------------------------------*/
double size, ratio, scale;
int jcol;
size = 1.0;
ratio = pow(rcond, (1.0 / (double)n));
nnza = 0;
/*---------------------------------------------------------------------
c Initialize colidx(n+1 .. 2n) to zero.
c Used by sprnvc to mark nonzero positions
c---------------------------------------------------------------------*/
for (i = 1; i <= n; i++) {
iv[n+i] = 0;
}
for (iouter = 1; iouter <= n; iouter++) {
nzv = nonzer;
sprnvc(n, nzv, v, colidx, iv, &iv[n]);
vecset(n, v, colidx, &nzv, iouter, 0.5);
//printf("nzv=%d\n",nzv);
for (ivelt = 1; ivelt <= nzv; ivelt++) {
jcol = colidx[ivelt];
//printf("jcol = %d\n",jcol);
if (jcol >= firstcol && jcol <= lastcol) {
scale = size * v[ivelt];
for (ivelt1 = 1; ivelt1 <= nzv; ivelt1++) {
irow = colidx[ivelt1];
if (irow >= firstrow && irow <= lastrow) {
nnza = nnza + 1;
//printf("INCR\n");
if (nnza > nz) {
printf("Space for matrix elements exceeded in"
" makea\n");
//printf("nnza, nzmax = %d, %d\n", nnza, nz);
//printf("iouter = %d\n", iouter);
exit(1);
}
//printf("nnza = %d\n",nnza);
acol[nnza] = jcol;
arow[nnza] = irow;
aelt[nnza] = v[ivelt1] * scale;
}
}
}
}
size = size * ratio;
}
/*---------------------------------------------------------------------
c ... add the identity * rcond to the generated matrix to bound
c the smallest eigenvalue from below by rcond
c---------------------------------------------------------------------*/
for (i = firstrow; i <= lastrow; i++) {
if (i >= firstcol && i <= lastcol) {
iouter = n + i;
nnza = nnza + 1;
if (nnza > nz) {
printf("Space for matrix elements exceeded in makea\n");
printf("nnza, nzmax = %d, %d\n", nnza, nz);
printf("iouter = %d\n", iouter);
exit(1);
}
acol[nnza] = i;
arow[nnza] = i;
aelt[nnza] = rcond - shift;
}
}
/*---------------------------------------------------------------------
c ... make the sparse matrix from list of elements with duplicates
c (v and iv are used as workspace)
c---------------------------------------------------------------------*/
sparse(a, colidx, rowstr, n, arow, acol, aelt,
firstrow, lastrow, v, iv, &iv[n], nnza);
for (int j = 1; j <= lastrow - firstrow + 1; j++) {
for (int k = rowstr[j]; k < rowstr[j+1]; k++) {
colidx[k] = colidx[k] - firstcol + 1;
}
}
}
/*---------------------------------------------------
c generate a sparse matrix from a list of
c [col, row, element] tri
c---------------------------------------------------*/
static void sparse(
double a[], /* a[1:*] */
int colidx[], /* colidx[1:*] */
int rowstr[], /* rowstr[1:*] */
int n,
int arow[], /* arow[1:*] */
int acol[], /* acol[1:*] */
double aelt[], /* aelt[1:*] */
int firstrow,
int lastrow,
double x[], /* x[1:n] */
boolean mark[], /* mark[1:n] */
int nzloc[], /* nzloc[1:n] */
int nnza)
/*---------------------------------------------------------------------
c rows range from firstrow to lastrow
c the rowstr pointers are defined for nrows = lastrow-firstrow+1 values
c---------------------------------------------------------------------*/
{
int nrows;
int i, j, jajp1, nza, k, nzrow;
double xi;
/*--------------------------------------------------------------------
c how many rows of result
c-------------------------------------------------------------------*/
nrows = lastrow - firstrow + 1;
/*--------------------------------------------------------------------
c ...count the number of triples in each row
c-------------------------------------------------------------------*/
for (j = 1; j <= n; j++) {
rowstr[j] = 0;
mark[j] = 0;
}
rowstr[n+1] = 0;
for (nza = 1; nza <= nnza; nza++) {
j = (arow[nza] - firstrow + 1) + 1;
rowstr[j] = rowstr[j] + 1;
}
rowstr[1] = 1;
for (j = 2; j <= nrows+1; j++) {
rowstr[j] = rowstr[j] + rowstr[j-1];
}
/*---------------------------------------------------------------------
c ... rowstr(j) now is the location of the first nonzero
c of row j of a
c---------------------------------------------------------------------*/
/*--------------------------------------------------------------------
c ... do a bucket sort of the triples on the row index
c-------------------------------------------------------------------*/
for (nza = 1; nza <= nnza; nza++) {
j = arow[nza] - firstrow + 1;
k = rowstr[j];
a[k] = aelt[nza];
colidx[k] = acol[nza];
rowstr[j] = rowstr[j] + 1;
}
/*--------------------------------------------------------------------
c ... rowstr(j) now points to the first element of row j+1
c-------------------------------------------------------------------*/
for (j = nrows; j >= 1; j--) {
rowstr[j+1] = rowstr[j];
}
rowstr[1] = 1;
/*--------------------------------------------------------------------
c ... generate the actual output rows by adding elements
c-------------------------------------------------------------------*/
nza = 0;
for (i = 1; i <= n; i++) {
x[i] = 0.0;
mark[i] = 0;
}
jajp1 = rowstr[1];
for (j = 1; j <= nrows; j++) {
nzrow = 0;
/*--------------------------------------------------------------------
c ...loop over the jth row of a
c-------------------------------------------------------------------*/
for (k = jajp1; k < rowstr[j+1]; k++) {
i = colidx[k];
x[i] = x[i] + a[k];
if ( mark[i] == 0 && x[i] != 0.0) {
mark[i] = 1;
nzrow = nzrow + 1;
nzloc[nzrow] = i;
}
}
/*--------------------------------------------------------------------
c ... extract the nonzeros of this row
c-------------------------------------------------------------------*/
for (k = 1; k <= nzrow; k++) {
i = nzloc[k];
mark[i] = 0;
xi = x[i];
x[i] = 0.0;
if (xi != 0.0) {
nza = nza + 1;
a[nza] = xi;
colidx[nza] = i;
}
}
jajp1 = rowstr[j+1];
rowstr[j+1] = nza + rowstr[1];
}
}
/*---------------------------------------------------------------------
c generate a sparse n-vector (v, iv)
c having nzv nonzeros
c
c mark(i) is set to 1 if position i is nonzero.
c mark is all zero on entry and is reset to all zero before exit
c this corrects a performance bug found by John G. Lewis, caused by
c reinitialization of mark on every one of the n calls to sprnvc
---------------------------------------------------------------------*/
static void sprnvc(
int n,
int nz,
double v[], /* v[1:*] */
int iv[], /* iv[1:*] */
int nzloc[], /* nzloc[1:n] */
int mark[] ) /* mark[1:n] */
{
int nn1;
int nzrow, nzv, ii, i;
double vecelt, vecloc;
nzv = 0;
nzrow = 0;
nn1 = 1;
do {
nn1 = 2 * nn1;
} while (nn1 < n);
/*--------------------------------------------------------------------
c nn1 is the smallest power of two not less than n
c-------------------------------------------------------------------*/
while (nzv < nz) {
vecelt = randlc(&tran, amult);
/*--------------------------------------------------------------------
c generate an integer between 1 and n in a portable manner
c-------------------------------------------------------------------*/
vecloc = randlc(&tran, amult);
i = icnvrt(vecloc, nn1) + 1;
if (i > n) continue;
/*--------------------------------------------------------------------
c was this integer generated already?
c-------------------------------------------------------------------*/
if (mark[i] == 0) {
mark[i] = 1;
nzrow = nzrow + 1;
nzloc[nzrow] = i;
nzv = nzv + 1;
v[nzv] = vecelt;
iv[nzv] = i;
}
}
for (ii = 1; ii <= nzrow; ii++) {
i = nzloc[ii];
mark[i] = 0;
}
}
/*---------------------------------------------------------------------
* scale a double precision number x in (0,1) by a power of 2 and chop it
*---------------------------------------------------------------------*/
static int icnvrt(double x, int ipwr2) {
return ((int)(ipwr2 * x));
}
/*--------------------------------------------------------------------
c set ith element of sparse vector (v, iv) with
c nzv nonzeros to val
c-------------------------------------------------------------------*/
static void vecset(
int n,
double v[], /* v[1:*] */
int iv[], /* iv[1:*] */
int *nzv,
int i,
double val)
{
int k;
boolean set;
set = 0;
for (k = 1; k <= *nzv; k++) {
if (iv[k] == i) {
v[k] = val;
set = 1;
}
}
if (!set) {
*nzv = *nzv + 1;
v[*nzv] = val;
iv[*nzv] = i;
}
}
void setup_proc_info(int num_procs) {
//c---------------------------------------------------------------------
//c num_procs must be a power of 2, and num_procs=num_proc_cols*num_proc_rows
//c When num_procs is not square, then num_proc_cols = 2*num_proc_rows
//c---------------------------------------------------------------------
//c First, number of procs must be power of two.
//c---------------------------------------------------------------------
// if( nprocs != num_procs ) {
// printf("Number of proceses not same as compiled\n");
// MPI_Abort(world, -1);
// }
int check = num_procs;
while (check % 2 == 0) {
check /= 2;
}
//int lognprocs = log2(num_procs);
// cover case 1 MPI proc only
//if (lognprocs == 0) lognprocs = 1;
if (check != 1) {
printf("Proc number not power of two, not dealing with this ...\n");
MPI_Abort(world, -1);
}
npcols = sqrt(num_procs); //lognprocs;
if (npcols * npcols == num_procs) {
nprows = npcols;
}
else {
//npcols = sqrt (num_procs/2);
//nprows = 2*npcols;
nprows = sqrt(num_procs/2);
npcols = 2*nprows;
if (npcols * nprows != num_procs) {
printf("I could not calculate npcols x nprows = num_procs, will abort!\n");
MPI_Abort(world, -1);
}
}
//nprows = sqrt(num_procs); //lognprocs;
//printf("(Rank %d): npcols %d, nprows %d\n", me, npcols, nprows);
}
void setup_submatrix_info(int * reduce_exch_proc, int * reduce_send_starts, int * reduce_send_lengths, int * reduce_recv_starts, int * reduce_recv_lengths) {
int proc_row = me / npcols;
int proc_col = me - proc_row*npcols;
int col_size, row_size;
// naa evenly distributed
if( (naa/npcols)*npcols == naa ) {
col_size = naa/npcols;
firstcol = proc_col*col_size + 1;
lastcol = firstcol - 1 + col_size;
row_size = naa/nprows;
firstrow = proc_row*row_size + 1;
lastrow = firstrow - 1 + row_size;
}
// not evenly distributed
else {
if( proc_row < naa - (naa/nprows)*nprows){
row_size = naa/nprows+ 1;
firstrow = proc_row*row_size + 1;
lastrow = firstrow - 1 + row_size;
}
else {
row_size = naa/nprows;
firstrow = (naa - naa/nprows*nprows)*(row_size+1)
+ (proc_row-(naa-naa/nprows*nprows))
*row_size + 1;
lastrow = firstrow - 1 + row_size;
}
if( npcols == nprows ) {
if( proc_col < naa - (naa/npcols)*npcols) {
col_size = naa/npcols+ 1;
firstcol = proc_col*col_size + 1;
lastcol = firstcol - 1 + col_size;
}
else {
col_size = naa/npcols;
firstcol = (naa - naa/npcols*npcols)*(col_size+1)
+ (proc_col-(naa-naa/npcols*npcols))
*col_size + 1;
lastcol = firstcol - 1 + col_size;
}
}
else {
if ((proc_col/2) < naa - naa/(npcols/2)*(npcols/2) ) {
col_size = naa/(npcols/2) + 1;
firstcol = (proc_col/2)*col_size + 1;
lastcol = firstcol - 1 + col_size;
}
else {
col_size = naa/(npcols/2);
firstcol = (naa - naa/(npcols/2)*(npcols/2)) *(col_size+1)
+ ((proc_col/2)-(naa-naa/(npcols/2)*(npcols/2)))
*col_size + 1;
lastcol = firstcol - 1 + col_size;
}
if( me % 2 == 0 ) {
lastcol = firstcol - 1 + (col_size-1)/2 + 1;
}
else {
firstcol = firstcol + (col_size-1)/2 + 1;
lastcol = firstcol - 1 + col_size/2;
}
}
}
if( npcols == nprows ) {
send_start = 1;
send_len = lastrow - firstrow + 1;
}
else {
if( me % 2 == 0 ) {
send_start = 1;
send_len = (1 + lastrow-firstrow+1)/2;
}
else {
send_start = (lastrow-firstrow)/2+1;
send_len = (lastrow-firstrow+1)/2;
}
}
// c---------------------------------------------------------------------
// c Transpose exchange processor
// c---------------------------------------------------------------------
if( npcols == nprows )
exch_proc = (me % nprows )*nprows + me/nprows;
else
exch_proc = 2*((me/2 % nprows )*nprows + me/2/nprows) + (me % 2);
//printf("(Rank %d): in setup, nprows=%d, npcols=%d, exch_proc = %d\n", me, nprows, npcols, exch_proc);
//c---------------------------------------------------------------------
//c Set up the reduce phase schedules...
//c---------------------------------------------------------------------
int i = npcols / 2;
l2npcols = 0;
while( i > 0 ) {
l2npcols++;
i /= 2;
}
int div_factor = npcols;
int j;
for (int i=0; i<l2npcols; i++) {
j = ((proc_col+div_factor/2) % div_factor) + proc_col / div_factor * div_factor;
reduce_exch_proc[i] = proc_row*npcols + j ;
//printf("(Rank %d): in setup, reduce_exch_proc[%d] = %d = %d * %d + %d\n", me, i, reduce_exch_proc[i], proc_row, npcols, j);
//fflush(stdout);
div_factor = div_factor / 2;
}
for (int i = l2npcols-1; i>=0; i--) {
if( nprows == npcols ) {
reduce_send_starts[i] = send_start;
reduce_send_lengths[i] = send_len;
reduce_recv_lengths[i] = lastrow - firstrow + 1;
}
else {
reduce_recv_lengths[i] = send_len;
if( i == l2npcols - 1) {
reduce_send_lengths[i] = (lastrow-firstrow+1-send_len);
if( me/2*2 == me )
reduce_send_starts[i] = send_start + send_len;
else
reduce_send_starts[i] = 1;
}
else
{
reduce_send_lengths[i] = send_len;
reduce_send_starts[i] = send_start;
}
}
reduce_recv_starts[i] = send_start;
}
exch_recv_length = lastcol - firstcol + 1;
}
void replay(double x[], double z[], double p[], double q[], double r[], double w[], bool failed, int reduce_exch_proc[], int reduce_send_starts[], int reduce_send_lengths[], double * rho) {
//printf("Rank %d: enter replay\n", me);
// All get the others' iteration number
int size;
MPI_Comm_size( world, &size);
killed_times++;