-
Notifications
You must be signed in to change notification settings - Fork 20
/
fsparse.c
executable file
·1688 lines (1478 loc) · 44.8 KB
/
fsparse.c
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
/* fsparse.c */
/* S. Engblom 2013-12-02 (OpenMP) */
/* S. Engblom 2010-02-02 (Minor revision) */
/* S. Engblom 2007-05-04 (Revision) */
/* S. Engblom 2005-05-05 (Revision) */
/* S. Engblom 2004-10-29 */
#include <math.h>
#include <string.h>
// temporary fix for CC under Solaris:
#ifndef NO_STDINT
#include <stdint.h>
#endif
#include "mex.h"
#include "matrix.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#ifndef FSPARSE_TIME
#define StartTime
#define StopTime
#define GetTime(dest)
#else
#include <sys/time.h>
static double *time_vec; // global dummy for output time vector
static struct timeval TIME_before,TIME_after;
#define StartTime gettimeofday(&TIME_before,NULL)
#define StopTime gettimeofday(&TIME_after,NULL)
#define GetTime(dest) ((dest) = (double)(TIME_after.tv_sec-TIME_before.tv_sec)+ \
(TIME_after.tv_usec-TIME_before.tv_usec)/1000000.0)
#endif // FSPARSE_TIME
// print intermediate results:
#undef PRINT_INTERMEDIATE
/*------------------------------------------------------------------------*/
// forward declarations
bool mx_IsInt(const mxArray *array_ptr);
bool getix(int **ix,int M,int N,int *max,bool nocopy,const mxArray *IX);
mxArray *sparse2sparse(const mxArray *S);
mxArray *full2sparse(const mxArray *S);
void squeeze(mxArray *S);
void sparse_insert(mwIndex *irS,double *prS,double *piS,
const int *irank,const int *rank,const mwSize *jrS,
const int *ii,
const double *sr,const double *si,
int smod,int sdiv,int len,int M);
void sparse_inserti(mwIndex *irS,double *prS,double *piS,
const int *irank,
const int *ii,int imod,
const double *sr,const double *si,
int smod,int sdiv,int len);
mxArray *sparse(const int *ii,const int *jj,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax);
mxArray *sparse_nosort(const int *ii,const int *jj,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax);
mxArray *gsparse(const int *ii,int imod,
const int *jj,int jdiv,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax);
mxArray *gsparse_nosort(const int *ii,int imod,
const int *jj,int jdiv,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax);
/*------------------------------------------------------------------------*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
#ifdef FSPARSE_TIME
// output time vector allocated here already
plhs[1] = mxCreateDoubleMatrix(1,6,mxREAL);
time_vec = mxGetPr(plhs[1]);
// check of syntax
if (nrhs < 1 || nrhs == 2 || 6 < nrhs || nlhs > 2)
mexErrMsgIdAndTxt("fsparse:e1",
"Expecting 1, 3..6 inputs and one or two outputs.");
#else
if (nrhs < 1 || nrhs == 2 || 6 < nrhs || nlhs > 1)
mexErrMsgIdAndTxt("fsparse:e1",
"Expecting 1, 3..6 inputs and one output.");
#endif
// special case for one input
if (nrhs == 1) {
if (!mxIsDouble(prhs[0]))
mexErrMsgIdAndTxt("fsparse:e2",
"Single input argument must be double.");
if (mxIsSparse(prhs[0])) {
plhs[0] = sparse2sparse(prhs[0]);
return;
}
else {
if (mxGetNumberOfDimensions(prhs[0]) > 2)
mexErrMsgIdAndTxt("fsparse:e3",
"Single input argument must be 2-D.");
plhs[0] = full2sparse(prhs[0]);
return;
}
}
if (!mx_IsInt(prhs[0]) && !mxIsDouble(prhs[0]) ||
mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]) ||
!mx_IsInt(prhs[1]) && !mxIsDouble(prhs[1]) ||
mxIsComplex(prhs[1]) || mxIsSparse(prhs[1]))
mexErrMsgIdAndTxt("fsparse:e4",
"Index argument must be real, double or integers "
"and non-sparse.");
if (!mxIsDouble(prhs[2]) || mxIsSparse(prhs[2]))
mexErrMsgIdAndTxt("fsparse:e5",
"Value argument must be double and non-sparse.");
if (mxGetNumberOfDimensions(prhs[0]) > 2 ||
mxGetNumberOfDimensions(prhs[1]) > 2 ||
mxGetNumberOfDimensions(prhs[2]) > 2)
mexErrMsgIdAndTxt("fsparse:e6","Input arguments must be 2-D.");
if (nrhs > 3) {
if (!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxIsSparse(prhs[3]))
mexErrMsgIdAndTxt("fsparse:e7","Size argument must be real, "
"double and non-sparse.");
if (nrhs > 4) {
if (mxGetNumberOfElements(prhs[4]) != 0 && !mxIsChar(prhs[4]))
mexErrMsgIdAndTxt("fsparse:e8","Flag argument must be "
"character array.");
if (nrhs > 5) {
// must determine the number of threads here (before any parallel region)
if (!mxIsDouble(prhs[5]) ||
mxGetNumberOfElements(prhs[5]) != 1)
mexErrMsgIdAndTxt("fsparse:e17","Number of threads must be a "
"real scalar.");
const int nthreads = (int)mxGetScalar(prhs[5]);
if (nthreads < 1)
mexErrMsgIdAndTxt("fsparse:e18","Number of threads must be >= 1.");
#ifdef _OPENMP
omp_set_num_threads(nthreads);
#endif // ignored otherwise
}
}
}
// input
const int Mii = mxGetM(prhs[0]),Nii = mxGetN(prhs[0]);
const int Mjj = mxGetM(prhs[1]),Njj = mxGetN(prhs[1]);
const bool nocopyii = mx_IsInt(prhs[0]),nocopyjj = mx_IsInt(prhs[1]);
int *ii,*jj;
const int Mss = mxGetM(prhs[2]),Nss = mxGetN(prhs[2]);
const double *sr = mxGetPr(prhs[2]);
const double *si = mxGetPi(prhs[2]);
const int len = Mii*Njj;
int M = 0,N = 0,Nzmax = -1,sort = 1;
// check of 'assembly' syntax
if (Nii != Njj && Nii != 1 ||
Mjj != Mii && Mjj != 1 ||
Mss != Mii && Mss != 1 ||
Nss != Njj && Nss != 1)
mexErrMsgIdAndTxt("fsparse:e9","Sizes mismatch.");
// input ii and jj
StartTime;
bool ok1,ok2;
#ifndef _OPENMP
ok1 = getix(&ii,Mii,Nii,&M,nocopyii,prhs[0]);
ok2 = getix(&jj,Mjj,Njj,&N,nocopyjj,prhs[1]);
#else
// independent calls
#pragma omp single nowait
ok1 = getix(&ii,Mii,Nii,&M,nocopyii,prhs[0]);
#pragma omp single nowait
ok2 = getix(&jj,Mjj,Njj,&N,nocopyjj,prhs[1]);
#pragma omp barrier
#endif // _OPENMP
if (!ok1 || !ok2)
mexErrMsgIdAndTxt("fsparse:e10","Index argument must be "
"nonnegative integers.");
StopTime;
GetTime(time_vec[0]);
// determine the input dimensions [M N Nzmax] of the output
if (nrhs > 3) {
const int szlen = mxGetNumberOfElements(prhs[3]);
const double *szval = mxGetPr(prhs[3]);
if (szlen > 0) {
if (szval[0] < 0.0 || szval[0] != ceil(szval[0]))
mexErrMsgIdAndTxt("fsparse:e11","Size argument must be "
"nonnegative integer.");
if (M > szval[0])
mexErrMsgIdAndTxt("fsparse:e12","Index exceeds matrix dimensions.");
M = szval[0];
if (szlen > 1) {
if (szval[1] < 0.0 || szval[1] != ceil(szval[1]))
mexErrMsgIdAndTxt("fsparse:e11","Size argument must be "
"nonnegative integer.");
if (N > szval[1])
mexErrMsgIdAndTxt("fsparse:e12","Index exceeds "
"matrix dimensions.");
N = szval[1];
if (szlen > 2) {
if (szval[2] < 0.0 || szval[2] != ceil(szval[2]))
mexErrMsgIdAndTxt("fsparse:e13","Nzmax argument must be "
"nonnegative integer.");
Nzmax = szval[2];
if (szlen > 3)
mexErrMsgIdAndTxt("fsparse:e14","Size argument must "
"contain 3 elements or less.");
}
}
}
}
// sorted/not sorted output
if (nrhs > 4) {
if (mxGetNumberOfElements(prhs[4]) == 0)
sort = 1;
else {
char buf[15]; // read 14 characters at most
if (mxGetString(prhs[4],buf,15) != 0)
mexErrMsgIdAndTxt("fsparse:e15","Unrecognized flag argument.");
if (strcmp(buf,"nosort") == 0)
sort = 0;
else if (strcmp(buf,"sort") != 0)
mexErrMsgIdAndTxt("fsparse:e15","Unrecognized flag argument.");
}
}
// empty case
if (len == 0) {
plhs[0] = mxCreateSparse(M,N,Nzmax == -1 ? 0 : Nzmax,
si == NULL ? mxREAL : mxCOMPLEX);
return;
}
if (Nii == Njj && Mjj == Mii) {
/* cases when ii and jj have the same shape but (sr,si) have one
of 4 different shapes */
const int smod = Nss != Njj ? Mii : len;
const int sdiv = Mss != Mii ? Mii : 1;
if (sort)
plhs[0] = sparse(ii,jj,sr,si,smod,sdiv,len,
M,N,Nzmax);
else
plhs[0] = sparse_nosort(ii,jj,sr,si,smod,sdiv,len,
M,N,Nzmax);
}
else {
// fully general case
const int imod = Nii != Njj ? Mii : len;
const int jdiv = Mjj != Mii ? Mii : 1;
const int smod = Nss != Njj ? Mii : len;
const int sdiv = Mss != Mii ? Mii : 1;
if (sort)
plhs[0] = gsparse(ii,imod,jj,jdiv,sr,si,smod,sdiv,len,
M,N,Nzmax);
else
plhs[0] = gsparse_nosort(ii,imod,jj,jdiv,sr,si,smod,sdiv,len,
M,N,Nzmax);
}
// deallocate
if (!nocopyii) mxFree(ii);
if (!nocopyjj) mxFree(jj);
// squeeze out zero elements
squeeze(plhs[0]);
}
/*------------------------------------------------------------------------*/
bool mx_IsInt(const mxArray *array_ptr)
/* Returns logical 1 (true) if array_ptr is a numeric array containing
integers (int8, int16, int32 or int64 depending on the platform),
and logical 0 (false) otherwise. This is useful since the test is
not provided in MEX.
In the name of the function, an extra underscore is used in order
to avoid confusing it with true MEX-functions. */
{
const int id = mxGetClassID(array_ptr);
const size_t siz = mxGetElementSize(array_ptr);
/* check that the class is an integer and that its size matches the
size of an int */
return (id == mxINT8_CLASS || id == mxINT16_CLASS ||
id == mxINT32_CLASS || id == mxINT64_CLASS) && siz == sizeof(int);
}
/*------------------------------------------------------------------------*/
bool getix(int **ix,int M,int N,int *max,bool nocopy,const mxArray *IX)
/* Gets indices ix from mxArray IX. The dimensions are M-by-N, max is
set to the maximum index and nocopy defines the type of IX (double
or int). */
{
bool ok = true;
int mx = *max;
#ifndef _OPENMP
if (nocopy) {
// no copy
const int *iix = (*ix = (int *)mxGetData(IX));
for (int i = 0; i < M*N; i++) {
if (iix[i] < 1)
return false;
if (iix[i] > mx) mx = iix[i];
}
}
else {
// typecast copy
const double *ival = mxGetPr(IX);
int *iix = (*ix = mxMalloc(M*N*sizeof(int)));
for (int i = 0; i < M*N; i++) {
if (ival[i] < 1.0 || ival[i] != ceil(ival[i]))
return false;
if ((iix[i] = ival[i]) > mx) mx = ival[i];
}
}
#else // _OPENMP
if (nocopy) {
// no copy
const int *iix = (*ix = (int *)mxGetData(IX));
#pragma omp parallel shared (mx)
{
int mymx = mx; // local version of mx
#pragma omp for
for (int i = 0; i < M*N; i++) {
if (iix[i] > mymx)
mymx = iix[i];
else if (iix[i] < 1)
ok = false; // no harm in continuing
}
if (mx < mymx)
#pragma omp critical
// ensure nothing changed, then make the swap:
if (mx < mymx) mx = mymx;
} // end omp parallel
}
else {
// typecast copy
const double *ival = mxGetPr(IX);
int *iix;
#pragma omp critical
// not thread-safe:
iix = (*ix = mxMalloc(M*N*sizeof(int)));
#pragma omp parallel shared (mx)
{
int mymx = mx; // local version of mx
#pragma omp for
for (int i = 0; i < M*N; i++) {
if (ival[i] < 1.0 || ival[i] != ceil(ival[i]))
ok = false; // no harm in continuing
else if ((iix[i] = ival[i]) > mymx)
mymx = ival[i];
}
if (mx < mymx)
#pragma omp critical
// ensure nothing changed, then make the swap:
if (mx < mymx) mx = mymx;
} // end omp parallel
}
#endif // _OPENMP
*max = mx;
return ok;
}
/*------------------------------------------------------------------------*/
mxArray *sparse2sparse(const mxArray *S)
/* Returns a deep copy T of a sparse matrix S. The allocation is
exact. */
{
const mwSize *jcS = mxGetJc(S);
const mwIndex *irS = mxGetIr(S);
const double *prS = mxGetPr(S),*piS = mxGetPi(S);
const int N = mxGetN(S),M = mxGetM(S),Nnz = jcS[N];
const bool real = piS == NULL;
mxArray *T;
/* straightforward */
T = mxCreateSparse(M,N,Nnz,real ? mxREAL : mxCOMPLEX);
memcpy(mxGetJc(T),jcS,(N+1)*sizeof(jcS[0]));
memcpy(mxGetIr(T),irS,Nnz*sizeof(irS[0]));
memcpy(mxGetPr(T),prS,Nnz*sizeof(prS[0]));
if (!real) memcpy(mxGetPi(T),piS,Nnz*sizeof(piS[0]));
return T;
}
/*------------------------------------------------------------------------*/
mxArray *full2sparse(const mxArray *A)
/* Constructs a sparse matrix S from a full matrix A. */
{
const double *prA = mxGetPr(A),*piA = mxGetPi(A);
const int N = mxGetN(A),M = mxGetM(A);
const bool real = piA == NULL;
mxArray *S;
if (real) {
mwSize *jcS = mxCalloc(N+1,sizeof(mwSize));
mwIndex *irS;
double *prS;
/* determine the column pointer */
for (int c = 1,k = 0; c <= N; c++,k += M)
for (int i = 0; i < M; i++)
if (prA[k+i] != 0.0) jcS[c]++;
for (int c = 2; c <= N; c++) jcS[c] += jcS[c-1];
/* allocate */
S = mxCreateSparse(0,0,jcS[N],mxREAL);
mxSetM(S,M);
mxSetN(S,N);
irS = mxGetIr(S);
prS = mxGetPr(S);
/* set the column pointer */
mxFree(mxGetJc(S));
mxSetJc(S,jcS);
/* copy data */
for (int c = 1,k = 0,dest = 0; c <= N; c++,k += M)
for (int i = 0; i < M; i++)
if (prA[k+i] != 0.0) {
irS[dest] = i;
prS[dest++] = prA[k+i];
}
}
else {
mwSize *jcS = mxCalloc(N+1,sizeof(mwSize));
mwIndex *irS;
double *prS,*piS;
for (int c = 1,k = 0; c <= N; c++,k += M)
for (int i = 0; i < M; i++)
if (prA[k+i] != 0.0 && piA[k+i] != 0.0) jcS[c]++;
for (int c = 2; c <= N; c++) jcS[c] += jcS[c-1];
S = mxCreateSparse(0,0,jcS[N],mxCOMPLEX);
mxSetM(S,M);
mxSetN(S,N);
irS = mxGetIr(S);
prS = mxGetPr(S);
piS = mxGetPi(S);
mxFree(mxGetJc(S));
mxSetJc(S,jcS);
for (int c = 1,k = 0,dest = 0; c <= N; c++,k += M)
for (int i = 0; i < M; i++)
if (prA[k+i] != 0.0 && piA[k+i] != 0.0) {
irS[dest] = i;
prS[dest] = prA[k+i];
piS[dest++] = piA[k+i];
}
}
return S;
}
/*------------------------------------------------------------------------*/
void squeeze(mxArray *S)
/* Removes any zero elements explicitly stored in the sparse matrix
S. No reallocation is performed.
There is a quite complicated and potentially faster algorithm based
on memmove() that performs the same operation. However, benchmark
tests indicate that the following simple code optimizes better. */
{
const int N = mxGetN(S);
mwSize *jcS = mxGetJc(S);
mwIndex *irS = mxGetIr(S);
double *prS = mxGetPr(S),*piS = mxGetPi(S);
if (piS == NULL) {
int c,i;
/* find the first zero, if any */
for (i = c = 0; c < N; c++)
for ( ; i < jcS[c+1]; i++)
if (prS[i] == 0.0)
goto rfound0; /* a 'double break' */
return;
rfound0:
/* copy in a conservative fashion */
for (int dest = i++; c < N; c++) {
for ( ; i < jcS[c+1]; i++)
if (prS[i] != 0.0) {
irS[dest] = irS[i];
prS[dest++] = prS[i];
}
jcS[c+1] = dest;
}
}
else {
int c,i;
for (i = c = 0; c < N; c++)
for ( ; i < jcS[c+1]; i++)
if (prS[i] == 0.0 && piS[i] == 0.0)
goto zfound0;
return;
zfound0:
for (int dest = i++; c < N; c++) {
for ( ; i < jcS[c+1]; i++)
if (prS[i] != 0.0 || piS[i] != 0.0) {
irS[dest] = irS[i];
prS[dest] = prS[i];
piS[dest++] = piS[i];
}
jcS[c+1] = dest;
}
}
}
/*------------------------------------------------------------------------*/
void sparse_insert(mwIndex *irS,double *prS,double *piS,
const int *irank,const int *rank,const mwIndex *jrS,
const int *ii,const double *sr,const double *si,
int smod,int sdiv,int len,int M)
/* Inserts elements into sparse matrix. Input is the sparse matrix
itself (irS,prS,piS), an index-table irank, the rowindices ii and
the values of the elements (sr,si). Four different formats of the
values are allowed as indicated by the parameters smod and sdiv.
Currently, input rank, jrS, and M are only used #ifdef _OPENMP and
for the full case 3 below. */
{
const bool real = si == NULL;
switch (2*(smod == len)+(sdiv == 1)) {
case 3 : /* full case */
#ifndef _OPENMP
if (real)
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += sr[i];
}
else
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += sr[i];
piS[irank[i]] += si[i];
}
#else // _OPENMP
if (real) {
if (rank != NULL) {
/* needed since not all cases respond to _OPENMP and uses a
different syntax */
#pragma omp parallel
{
const int nThreads = omp_get_num_threads();
const int myId = omp_get_thread_num();
const int rstart = 1+M*myId/nThreads;
const int rend = M*(myId+1)/nThreads;
int istart;
if (rstart == 1)
istart = 0;
else
istart = jrS[rstart-1];
if (rend >= 1) {
for (int i = istart; i < jrS[rend]; i++) irS[irank[i]] = ii[rank[i]]-1;
for (int i = istart; i < jrS[rend]; i++) prS[irank[i]] += sr[rank[i]];
}
} // end parallel
}
else {
#pragma omp single nowait
for (int i = 0; i < len; i++) irS[irank[i]] = ii[i]-1;
#pragma omp single nowait
for (int i = 0; i < len; i++) prS[irank[i]] += sr[i];
}
}
else {
#pragma omp parallel
{
if (rank != NULL) {
const int nThreads = omp_get_num_threads();
const int myId = omp_get_thread_num();
const int rstart = 1+M*myId/nThreads;
const int rend = M*(myId+1)/nThreads;
int istart;
if (rstart == 1)
istart = 0;
else
istart = jrS[rstart-1];
if (rend >= 1)
for (int i = istart; i < jrS[rend]; i++) {
irS[irank[i]] = ii[rank[i]]-1;
prS[irank[i]] += sr[rank[i]];
piS[irank[i]] += si[rank[i]];
}
}
else {
#pragma omp single nowait
for (int i = 0; i < len; i++) irS[irank[i]] = ii[i]-1;
#pragma omp single nowait
for (int i = 0; i < len; i++) prS[irank[i]] += sr[i];
#pragma omp single nowait
for (int i = 0; i < len; i++) piS[irank[i]] += si[i];
}
} // end parallel
}
#endif // _OPENMP
break;
case 2 : /* horizontal case */
if (real)
for (int j = 0; j < len; j += sdiv) {
const double ssr = sr[j/sdiv];
for (int i = j; i < j+sdiv; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += ssr;
}
}
else
for (int j = 0; j < len; j += sdiv) {
const double ssr = sr[j/sdiv];
const double ssi = si[j/sdiv];
for (int i = j; i < j+sdiv; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += ssr;
piS[irank[i]] += ssi;
}
}
break;
case 1 : /* vertical case */
if (real)
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += sr[i%smod];
}
else
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += sr[i%smod];
piS[irank[i]] += si[i%smod];
}
break;
case 0 : /* scalar case */
#ifndef _OPENMP
if (real) {
const double ssr = sr[0];
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += ssr;
}
}
else {
const double ssr = sr[0];
const double ssi = si[0];
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i]-1;
prS[irank[i]] += ssr;
piS[irank[i]] += ssi;
}
}
#else // _OPENMP
if (real) {
#pragma omp parallel
{
const double ssr = sr[0];
#pragma omp single nowait
for (int i = 0; i < len; i++) irS[irank[i]] = ii[i]-1;
#pragma omp single nowait
for (int i = 0; i < len; i++) prS[irank[i]] += ssr;
} // end omp parallel
}
else {
#pragma omp parallel
{
const double ssr = sr[0];
const double ssi = si[0];
#pragma omp single nowait
for (int i = 0; i < len; i++) irS[irank[i]] = ii[i]-1;
#pragma omp single nowait
for (int i = 0; i < len; i++) prS[irank[i]] += ssr;
#pragma omp single nowait
for (int i = 0; i < len; i++) piS[irank[i]] += ssi;
} // end omp parallel
}
#endif // _OPENMP
break;
}
}
/*------------------------------------------------------------------------*/
void sparse_inserti(mwIndex *irS,double *prS,double *piS,
const int *irank,
const int *ii,int imod,
const double *sr,const double *si,
int smod,int sdiv,int len)
/* Same as sparse_insert() above except that ii is assumed to be
vertical. */
{
const bool real = si == NULL;
switch (2*(smod == len)+(sdiv == 1)) {
case 3 : /* full case */
if (real)
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i%imod]-1;
prS[irank[i]] += sr[i];
}
else
for (int i = 0; i < len; i++) {
irS[irank[i]] = ii[i%imod]-1;
prS[irank[i]] += sr[i];
piS[irank[i]] += si[i];
}
break;
case 2 : /* horizontal case */
if (real)
for (int j = 0; j < len; j += sdiv) {
const double ssr = sr[j/sdiv];
for (int i = 0; i < imod; i++) {
irS[irank[j+i]] = ii[i]-1;
prS[irank[j+i]] += ssr;
}
}
else
for (int j = 0; j < len; j += sdiv) {
const double ssr = sr[j/sdiv];
const double ssi = si[j/sdiv];
for (int i = 0; i < imod; i++) {
irS[irank[j+i]] = ii[i]-1;
prS[irank[j+i]] += ssr;
piS[irank[j+i]] += ssi;
}
}
break;
case 1 : /* vertical case */
if (real)
for (int i = 0; i < imod; i++) {
const double ssr = sr[i];
const int iii = ii[i]-1;
for (int j = i; j < len; j += imod) {
irS[irank[j]] = iii;
prS[irank[j]] += ssr;
}
}
else
for (int i = 0; i < imod; i++) {
const double ssr = sr[i];
const double ssi = si[i];
const int iii = ii[i]-1;
for (int j = i; j < len; j += imod) {
irS[irank[j]] = iii;
prS[irank[j]] += ssr;
piS[irank[j]] += ssi;
}
}
break;
case 0 : /* scalar case */
if (real) {
const double ssr = sr[0];
for (int j = 0; j < len; j += imod)
for (int i = 0; i < imod; i++) {
irS[irank[j+i]] = ii[i]-1;
prS[irank[j+i]] += ssr;
}
}
else {
const double ssr = sr[0];
const double ssi = si[0];
for (int j = 0; j < len; j += imod)
for (int i = 0; i < imod; i++) {
irS[irank[j+i]] = ii[i]-1;
prS[irank[j+i]] += ssr;
piS[irank[j+i]] += ssi;
}
}
break;
}
}
/*------------------------------------------------------------------------*/
#ifndef _OPENMP
mxArray *sparse(const int *ii,const int *jj,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax)
/* Constructs a sparse matrix in Compressed Column Storage (CCS) from
triplet format [ii,jj,sr(si)]. An ordinary Matlab sparse matrix is
thus constructed.
Input (smod,sdiv) determine the shape of the value array sr(si) and
len is the length of the index arrays which both must have the same
shape. [M N Nzmax] determine the dimensions of the resulting
matrix. If Nzmax = -1, then the allocation is exact (i.e. nnz(S) =
nzmax(S)). Otherwise, Nzmax must be greater than or equal to the
number of nonzeros needed to be stored.
The memory demand of the algorithm is (at peak and for sufficiently
large output) <input>+int[len]+<output>. */
{
// output
mxArray *S;
mwSize *jcS; // column pointer for sparse matrix S
int *irank; // inverse rank array of length len
mwSize *jrS; // accumulated "pessimistic" row counter
int *rank; // rank-array for rows
int *hcol; // cache memory for columns
// Part 1: count and accumulate indices to rows
StartTime;
jrS = mxCalloc(M+1,sizeof(jrS[0]));
for (int i = 0; i < len; i++) jrS[ii[i]]++;
for (int r = 2; r <= M; r++) jrS[r] += jrS[r-1];
StopTime;
GetTime(time_vec[1]);
#ifdef PRINT_INTERMEDIATE
mexPrintf("jrS = [");
for (int r = 0; r <= M; r++) mexPrintf("%d,",jrS[r]);
mexPrintf("]\n\n");
#endif
// Part 2: build rank with the active use of jrS
StartTime;
rank = mxMalloc(len*sizeof(rank[0]));
jrS--; /* (unit-offset in ii) */
for (int i = 0; i < len; i++) rank[jrS[ii[i]]++] = i;
// rank now allows for row-wise traversal
StopTime;
GetTime(time_vec[2]);
#ifdef PRINT_INTERMEDIATE
mexPrintf("rank = [");
for (int i = 0; i < len; i++) mexPrintf("%d,",rank[i]);
mexPrintf("]\n");
mexPrintf("jrS = [*,");
for (int r = 1; r <= M+1; r++) mexPrintf("%d,",jrS[r]);
mexPrintf("]\n\n");
#endif
/* Part 3: loop over input and make each column unique with respect
to rowindices, building both an index vector irank and the final
column pointer at the same time */
StartTime;
jcS = mxCalloc(N+1,sizeof(jcS[0]));
hcol = mxCalloc(N,sizeof(hcol[0]));
hcol--; /* (unit-offset in jj) */
irank = mxMalloc(len*sizeof(irank[0]));
for (int row = 1,i = 0; row <= M; row++)
for ( ; i < jrS[row]; i++) {
const int ixijs = rank[i]; // index into input data triplet (ii,jj,sr)
const int col = jj[ixijs]; // column index
// new element?
if (hcol[col] < row) {
hcol[col] = row; // remembered by the row index
jcS[col]++; // count it
}
// irank keeps track of where it should go
irank[ixijs] = jcS[col]-1;
}
mxFree(++hcol);
mxFree(rank);
mxFree(++jrS);
StopTime;
GetTime(time_vec[3]);
#ifdef PRINT_INTERMEDIATE
mexPrintf("irank = [");
for (int i = 0; i < len; i++) mexPrintf("%d,",irank[i]);
mexPrintf("]\n");
mexPrintf("jcS = [");
for (int c = 0; c <= N; c++) mexPrintf("%d,",jcS[c]);
mexPrintf("]\n\n");
#endif
// Part 4: accumulate pointer to columns
StartTime;
for (int c = 2; c <= N; c++) jcS[c] += jcS[c-1];
// irank must account for the previous accumulation
jcS--; /* (again, unit-offset in jj) */
for (int i = 0; i < len; i++) irank[i] += jcS[jj[i]];
jcS++;
StopTime;
GetTime(time_vec[4]);
#ifdef PRINT_INTERMEDIATE
mexPrintf("irank = [");
for (int i = 0; i < len; i++) mexPrintf("%d,",irank[i]);
mexPrintf("]\n");
mexPrintf("jcS = [");
for (int c = 0; c <= N; c++) mexPrintf("%d,",jcS[c]);
mexPrintf("]\n\n");
#endif
// allocate output
if (Nzmax == -1)
Nzmax = jcS[N];
else if (Nzmax < jcS[N]) {
mxFree(irank);
mxFree(jcS);
mexErrMsgIdAndTxt("fsparse:e16","Allocation limited by caller: "
"sparse matrix does not fit.");
}
S = mxCreateSparse(0,0,Nzmax,si == NULL ? mxREAL : mxCOMPLEX);
mxSetM(S,M);
mxSetN(S,N);
// set the column pointer
mxFree(mxGetJc(S));
mxSetJc(S,jcS);
// insert the data
StartTime;
sparse_insert(mxGetIr(S),mxGetPr(S),mxGetPi(S),
irank,0,0,ii,sr,si,smod,sdiv,len,M);
StopTime;
GetTime(time_vec[5]);
mxFree(irank);
return S;
}
/*------------------------------------------------------------------------*/
#else // _OPENMP
/*------------------------------------------------------------------------*/
mxArray *sparse(const int *ii,const int *jj,
const double *sr,const double *si,
int smod,int sdiv,
int len,int M,int N,int Nzmax)
/* This is the OpenMP-version of the sparse function above. */
{
// output
mxArray *S;
mwSize **jcS; // column pointer, one per thread
mwSize *jcS_; // final column pointer
int *irank; // inverse rank array of length len
int *irankP; // permuted version of irank
mwSize **jrS; // accumulated "pessimistic" row counter
int *rank; // rank-array for rows
// Part 1: count and accumulate indices to rows
StartTime;
const int nThreads = omp_get_max_threads();
jrS = mxMalloc((nThreads+1)*sizeof(jrS[0]));
for (int k = 0; k <= nThreads; k++) {
jrS[k] = mxCalloc(M+1,sizeof(jrS[k][0]));
jrS[k]--; /* (unit-offset in ii) */
}
#pragma omp parallel
{
const int myId = omp_get_thread_num();
const int istart = len*myId/nThreads;
const int iend = len*(myId+1)/nThreads;
for (int i = istart; i < iend; i++)
jrS[myId+1][ii[i]]++;
#pragma omp barrier
// accumulate jrS over the threads
#pragma omp for
for (int r = 1; r <= M; r++)
for (int k = 1; k < nThreads; k++)
jrS[k+1][r] += jrS[k][r];
// serial accumulation in jrS[0]
#pragma omp single
for (int r = 1; r <= M; r++)
jrS[0][r+1] += jrS[0][r]+jrS[nThreads][r];
// determine a private jrS for each thread
#pragma omp for
for (int r = 1; r <= M; r++)
for (int k = 1; k < nThreads; k++)
jrS[k][r] += jrS[0][r];
} // end parallel
StopTime;
GetTime(time_vec[1]);
// Part 2: build rank with the active use of jrS
StartTime;
rank = mxMalloc(len*sizeof(rank[0]));