-
Notifications
You must be signed in to change notification settings - Fork 1
/
multidim_array.h
4262 lines (3835 loc) · 124 KB
/
multidim_array.h
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
/***************************************************************************
*
* Author: "Sjors H.W. Scheres"
* MRC Laboratory of Molecular Biology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This complete copyright notice must be included in any revised version of the
* source code. Additional authorship citations may be added, but existing
* author citations must be preserved.
***************************************************************************/
/***************************************************************************
*
* Authors: Carlos Oscar S. Sorzano ([email protected])
*
* Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* All comments concerning this program package may be sent to the
* e-mail address '[email protected]'
***************************************************************************/
#ifndef MULTIDIM_ARRAY_H
#define MULTIDIM_ARRAY_H
#include <typeinfo>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include "src/funcs.h"
#include "src/error.h"
#include "src/args.h"
#include "src/matrix1d.h"
#include "src/matrix2d.h"
#include "src/complex.h"
extern int bestPrecision(float F, int _width);
extern std::string floatToString(float F, int _width, int _prec);
/// @defgroup MultidimensionalArrays Multidimensional Arrays
/// @ingroup DataLibrary
//@{
/** @name MultidimArraysSpeedUp Speed up macros
*
* This macros are defined to allow high speed in critical parts of your
* program. They shouldn't be used systematically as usually there is no
* checking on the correctness of the operation you are performing. Speed comes
* from three facts: first, they are macros and no function call is performed
* (although most of the critical functions are inline functions), there is no
* checking on the correctness of the operation (it could be wrong and you are
* not warned of it), and destination vectors are not returned saving time in
* the copy constructor and in the creation/destruction of temporary vectors.
*/
//@{
/** Returns the first X valid logical index
*/
#define STARTINGX(v) ((v).xinit)
/** Returns the last X valid logical index
*/
#define FINISHINGX(v) ((v).xinit + (v).xdim - 1)
/** Returns the first Y valid logical index
*/
#define STARTINGY(v) ((v).yinit)
/** Returns the last Y valid logical index
*/
#define FINISHINGY(v) ((v).yinit + (v).ydim - 1)
/** Returns the first Z valid logical index
*/
#define STARTINGZ(v) ((v).zinit)
/** Returns the last Z valid logical index
*/
#define FINISHINGZ(v) ((v).zinit + (v).zdim - 1)
/** Access to X dimension (size)
*/
#define XSIZE(v) ((v).xdim)
/** Access to Y dimension (size)
*/
#define YSIZE(v) ((v).ydim)
/** Access to Z dimension (size)
*/
#define ZSIZE(v) ((v).zdim)
/** Access to N dimension (size)
*/
#define NSIZE(v) ((v).ndim)
/** Access to XY dimension (Ysize*Xsize)
*/
#define YXSIZE(v) ((v).yxdim)
/** Access to XYZ dimension (Zsize*Ysize*Xsize)
*/
#define ZYXSIZE(v) ((v).zyxdim)
/** Access to XYZN dimension (Nsize*Zsize*Ysize*Xsize)
*/
#define MULTIDIM_SIZE(v) ((v).nzyxdim)
/** Access to XYZN dimension (Nsize*Zsize*Ysize*Xsize)
*/
#define NZYXSIZE(v) ((v).nzyxdim)
/** Array access.
*
* This macro gives you access to the array (T **)
*/
#ifndef MULTIDIM_ARRAY
#define MULTIDIM_ARRAY(v) ((v).data)
#endif
/** Access to a direct element.
* v is the array, l is the image, k is the slice, i is the Y index and j is the X index.
* i and j) within the slice.
*/
#define DIRECT_NZYX_ELEM(v, l, k, i, j) ((v).data[(l)*ZYXSIZE(v)+(k)*YXSIZE(v)+((i)*XSIZE(v))+(j)])
/** Multidim element: Logical access.
*/
#define NZYX_ELEM(v, l, k, i, j) \
DIRECT_NZYX_ELEM((v), (l), (k) - STARTINGZ(v), (i) - STARTINGY(v), (j) - STARTINGX(v))
/** Access to a direct element.
* v is the array, k is the slice and n is the number of the pixel (combined i and j)
* within the slice.
*/
#define DIRECT_MULTIDIM_ELEM(v,n) ((v).data[(n)])
/** For all direct elements in the array
*
* This macro is used to generate loops for the array in an easy manner. It
* defines an internal index 'n' which goes over the slices and 'n' that
* goes over the pixels in each slice.
*
* @code
* FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
* {
* std::cout << DIRECT_MULTIDIM_ELEM(v,n) << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v) \
for (long int n=0; n<NZYXSIZE(v); ++n)
/** For all direct elements in the array
*
* This macro is used to generate loops for the array in an easy
* manner. It defines internal indexes 'l', 'k','i' and 'j' which
* ranges over the n volume using its physical definition.
*
* @code
* FOR_ALL_DIRECT_NZYX_ELEMENTS_IN_MULTIDIMARRAY(v)
* {
* std::cout << DIRECT_NZYX_ELEM(v,l, k, i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_NZYX_ELEMENTS_IN_MULTIDIMARRAY(V) \
for (long int l=0; l<NSIZE(V); l++) \
for (long int k=0; k<ZSIZE(V); k++) \
for (long int i=0; i<YSIZE(V); i++) \
for (long int j=0; j<XSIZE(V); j++)
/** For all direct elements in the array
*
* This macro is used to generate loops for the array in an easy
* manner. It defines internal indexes 'l', 'k','i' and 'j' which
* ranges over the n volume using its logical definition.
*
* @code
* FOR_ALL_NZYX_ELEMENTS_IN_MULTIDIMARRAY(v)
* {
* std::cout << NZYX_ELEM(v,l, k, i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_NZYX_ELEMENTS_IN_MULTIDIMARRAY(V) \
for (long int l=0; l<NSIZE(V); l++) \
for (long int k=STARTINGZ(V); k<=FINISHINGZ(V); k++) \
for (long int i=STARTINGY(V); i<=FINISHINGY(V); i++) \
for (long int j=STARTINGX(V); j<=FINISHINGX(V); j++)
/** For all direct elements in the array, pointer version
*
* This macro is used to generate loops for the array in an easy manner. It
* defines an internal index 'k' which goes over the slices and 'n' that
* goes over the pixels in each slice. Each element can be accessed through
* an external pointer called ptr.
*
* @code
* T* ptr=NULL;
* long int n;
* FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY_ptr(v,n,ptr)
* {
* std::cout << *ptr << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY_ptr(v,n,ptr) \
for ((n)=0, (ptr)=(v).data; (n)<NZYXSIZE(v); ++(n), ++(ptr))
/** Access to a direct element.
* v is the array, k is the slice (Z), i is the Y index and j is the X index.
*/
#define DIRECT_A3D_ELEM(v,k,i,j) ((v).data[(k)*YXSIZE(v)+((i)*XSIZE(v))+(j)])
/** A short alias for the previous function.
*
*/
#define dAkij(V, k, i, j) DIRECT_A3D_ELEM(V, k, i, j)
/** Volume element: Logical access.
*
* @code
* A3D_ELEM(V, -1, -2, 1) = 1;
* val = A3D_ELEM(V, -1, -2, 1);
* @endcode
*/
#define A3D_ELEM(V, k, i, j) \
DIRECT_A3D_ELEM((V),(k) - STARTINGZ(V), (i) - STARTINGY(V), (j) - STARTINGX(V))
/** For all elements in the array.
*
* This macro is used to generate loops for the volume in an easy way. It
* defines internal indexes 'k','i' and 'j' which ranges the volume using its
* mathematical definition (ie, logical access).
*
* @code
* FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
* {
* std::cout << V(k, i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V) \
for (long int k=STARTINGZ(V); k<=FINISHINGZ(V); k++) \
for (long int i=STARTINGY(V); i<=FINISHINGY(V); i++) \
for (long int j=STARTINGX(V); j<=FINISHINGX(V); j++)
/** For all direct elements in the array.
*
* This macro is used to generate loops for the volume in an easy way. It
* defines internal indexes 'k','i' and 'j' which ranges the volume using its
* physical definition.
*
* @code
* FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V)
* {
* std::cout << DIRECT_A3D_ELEM(m, k, i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V) \
for (long int k=0; k<ZSIZE(V); k++) \
for (long int i=0; i<YSIZE(V); i++) \
for (long int j=0; j<XSIZE(V); j++)
/** Access to a direct element of a matrix.
* v is the array, i and j define the element v_ij.
*
* Be careful because this is physical access, usually matrices follow the C
* convention of starting index==0 (X and Y). This function should not be used
* as it goes against the vector library philosophy unless you explicitly want
* to access directly to any value in the matrix without taking into account its
* logical position
*
* @code
* DIRECT_A2D_ELEM(m, 0, 0) = 1;
* val = DIRECT_A2D_ELEM(m, 0, 0);
* @endcode
*/
#define DIRECT_A2D_ELEM(v,i,j) ((v).data[(i)*(v).xdim+(j)])
/** Short alias for DIRECT_A2D_ELEM
*/
#define dAij(M, i, j) DIRECT_A2D_ELEM(M, i, j)
/** Matrix element: Logical access
*
* @code
* A2D_ELEM(m, -2, 1) = 1;
* val = A2D_ELEM(m, -2, 1);
* @endcode
*/
#define A2D_ELEM(v, i, j) \
DIRECT_A2D_ELEM(v, (i) - STARTINGY(v), (j) - STARTINGX(v))
/** TRUE if both arrays have the same shape
*
* Two arrays have the same shape if they have the same size and the same
* starting point. Be aware that this is a macro which simplifies to a boolean.
*/
#define SAME_SHAPE2D(v1, v2) \
(XSIZE(v1) == XSIZE(v2) && \
YSIZE(v1) == YSIZE(v2) && \
STARTINGX(v1) == STARTINGX(v2) && \
STARTINGY(v1) == STARTINGY(v2))
/** For all elements in the array
*
* This macro is used to generate loops for the matrix in an easy way. It
* defines internal indexes 'i' and 'j' which ranges the matrix using its
* mathematical definition (ie, logical access).
*
* @code
* FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
* {
* std::cout << m(i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m) \
for (long int i=STARTINGY(m); i<=FINISHINGY(m); i++) \
for (long int j=STARTINGX(m); j<=FINISHINGX(m); j++)
/** For all elements in the array, accessed physically
*
* This macro is used to generate loops for the matrix in an easy way using
* physical indexes. It defines internal indexes 'i' and 'j' which ranges the
* matrix using its physical definition.
*
* @code
* FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY2D(m)
* {
* std::cout << DIRECT_A2D_ELEM(m, i, j) << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY2D(m) \
for (long int i=0; i<YSIZE(m); i++) \
for (long int j=0; j<XSIZE(m); j++)
/** Vector element: Physical access
*
* Be careful because this is physical access, usually vectors follow the C
* convention of starting index==0. This function should not be used as it goes
* against the vector library philosophy unless you explicitly want to access
* directly to any value in the vector without taking into account its logical
* position.
*
* @code
* DIRECT_A1D_ELEM(v, 0) = 1;
* val = DIRECT_A1D_ELEM(v, 0);
* @endcode
*/
#define DIRECT_A1D_ELEM(v, i) ((v).data[(i)])
/** A short alias to previous function
*/
#define dAi(v, i) DIRECT_A1D_ELEM(v, i)
/** Vector element: Logical access
*
* @code
* A1D_ELEM(v, -2) = 1;
* val = A1D_ELEM(v, -2);
* @endcode
*/
#define A1D_ELEM(v, i) DIRECT_A1D_ELEM(v, (i) - ((v).xinit))
/** For all elements in the array
*
* This macro is used to generate loops for the vector in an easy manner. It
* defines an internal index 'i' which ranges the vector using its mathematical
* definition (ie, logical access).
*
* @code
* FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
* {
* std::cout << v(i) << " ";
* }
* @endcode
*/
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v) \
for (long int i=STARTINGX(v); i<=FINISHINGX(v); i++)
/** For all elements in the array, accessed physically
*
* This macro is used to generate loops for the vector in an easy way using
* physical indexes. It defines internal the index 'i' which ranges the vector
* using its physical definition.
*
* @code
* FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v)
* {
* std::cout << DIRECT_A1D_ELEM(v, i) << " ";
* }
* @endcode
*/
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v) \
for (long int i=0; i<v.xdim; i++)
//@}
// Forward declarations ====================================================
template<typename T>
class MultidimArray;
template<typename T>
void coreArrayByScalar(const MultidimArray<T>& op1, const T& op2,
MultidimArray<T>& result, char operation);
template<typename T>
void coreScalarByArray(const T& op1, const MultidimArray<T>& op2,
MultidimArray<T>& result, char operation);
template<typename T>
void coreArrayByArray(const MultidimArray<T>& op1, const MultidimArray<T>& op2,
MultidimArray<T>& result, char operation);
/** Template class for Xmipp arrays.
* This class provides physical and logical access.
*/
template<typename T>
class MultidimArray
{
public:
/* The array itself.
The array is always a 3D array (Z,Y,X). For vectors the size of the array
is (1,1,X) and for matrices (1,Y,X). The pixel (i,j) (y,x) is at the
position data[i*Xdim+j] or data[y*Xdim+x]
*/
T* data;
// Destroy data
bool destroyData;
// Number of images
long int ndim;
// Number of elements in Z
long int zdim;
// Number of elements in Y
long int ydim;
// Number of elements in X
long int xdim;
// Number of elements in YX
long int yxdim;
// Number of elements in ZYX
long int zyxdim;
// Number of elements in NZYX
long int nzyxdim;
// Z init
long int zinit;
// Y init
long int yinit;
// X init
long int xinit;
//Alloc memory or map to a file
bool mmapOn;
// Mapped File name
FileName mapFile;
//Mapped file handler
int mFd;
// Number of elements in NZYX in allocated memory
long int nzyxdimAlloc;
public:
/// @name Constructors
//@{
void *operator new(size_t size)
{
return _mm_malloc(size, 64);
}
void operator delete(void *p)
{
_mm_free(p);
}
/** Empty constructor.
* The empty constructor creates an array with no memory associated,
* size=0.
*/
MultidimArray()
{
coreInit();
}
/** Size constructor with 4D size.
* The Size constructor creates an array with memory associated,
* and fills it with zeros.
*/
MultidimArray(long int Ndim, long int Zdim, long int Ydim, long int Xdim)
{
coreInit();
resize(Ndim, Zdim, Ydim, Xdim);
}
/** Size constructor with 3D size.
* The Size constructor creates an array with memory associated,
* and fills it with zeros.
*/
MultidimArray(long int Zdim, long int Ydim, long int Xdim)
{
coreInit();
resize(1, Zdim, Ydim, Xdim);
}
/** Size constructor with 2D size.
* The Size constructor creates an array with memory associated,
* and fills it with zeros.
*/
MultidimArray(long int Ydim, long int Xdim)
{
coreInit();
resize(1, 1, Ydim, Xdim);
}
/** Size constructor with 1D size.
* The Size constructor creates an array with memory associated,
* and fills it with zeros.
*/
MultidimArray(long int Xdim)
{
coreInit();
resize(1, 1, 1, Xdim);
}
/** Copy constructor
*
* The created volume is a perfect copy of the input array but with a
* different memory assignment.
*
* @code
* MultidimArray< DOUBLE > V2(V1);
* @endcode
*/
MultidimArray(const MultidimArray<T>& V)
{
coreInit();
*this = V;
}
/** Copy constructor from a Matrix1D.
* The Size constructor creates an array with memory associated,
* and fills it with zeros.
*/
MultidimArray(const Matrix1D<T>& V)
{
coreInit();
resize(1, 1, 1, V.size());
for (long int i = 0; i < V.size(); i++)
(*this)(i) = V(i);
}
/** Constructor from vector 1D
* This will create a MultidimArray 1D
* the size and elements will be copied from
* the std::vector
*/
MultidimArray(const std::vector<T> &vector)
{
coreInit();
resize(1, 1, 1, vector.size());
for (long int i = 0; i < vector.size(); i++)
(*this)(i) = vector[i];
}
/** Destructor.
*/
~MultidimArray()
{
coreDeallocate();
}
/** Clear.
*/
void clear()
{
coreDeallocate();
coreInit();
}
//@}
/// @name Core memory operations
//@{
/** Core init.
* Initialize everything to 0
*/
void coreInit()
{
xdim=0;
yxdim=0;
zyxdim=0;
nzyxdim=0;
ydim=1;
zdim=1;
ndim=1;
zinit=0;
yinit=0;
xinit=0;
data=NULL;
nzyxdimAlloc = 0;
destroyData=true;
mmapOn = false;
mFd=0;
}
/** Core allocate with dimensions.
*/
void coreAllocate(long int _ndim, long int _zdim, long int _ydim, long int _xdim)
{
if (_ndim <= 0 || _zdim <= 0 || _ydim<=0 || _xdim<=0)
{
clear();
return;
}
if(data!=NULL)
REPORT_ERROR( "do not allocate space for an image if you have not deallocate it first");
ndim=_ndim;
zdim=_zdim;
ydim=_ydim;
xdim=_xdim;
yxdim=ydim*xdim;
zyxdim=zdim*yxdim;
nzyxdim=ndim*zyxdim;
coreAllocate();
}
/** Core allocate without dimensions.
*
* It is supposed the dimensions are set previously with setXdim(x), setYdim(y)
* setZdim(z), setNdim(n) or with setDimensions(Xdim, Ydim, Zdim, Ndim);
*
*/
void coreAllocate()
{
if(data!=NULL)
REPORT_ERROR( "do not allocate space for an image if you have not deallocate it first");
if (nzyxdim < 0)
REPORT_ERROR("coreAllocate:Cannot allocate a negative number of bytes");
if (mmapOn)
{
mapFile.initRandom(8);
mapFile = mapFile.addExtension("tmp");
if ( ( mFd = open(mapFile.c_str(), O_RDWR | O_CREAT | O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) ) == -1 )
REPORT_ERROR("MultidimArray::coreAllocate: Error creating map file.");
if ((lseek(mFd, nzyxdim*sizeof(T), SEEK_SET) == -1)|| (::write(mFd,"",1) == -1))// Use of :: to call write from global space due to confict with multidimarray::write
{
close(mFd);
REPORT_ERROR("MultidimArray::coreAllocate: Error 'stretching' the map file.");
}
if ( (data = (T*) mmap(0,nzyxdim*sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0)) == (void*) -1 )
REPORT_ERROR("MultidimArray::coreAllocate: mmap failed.");
}
else
{
data = new T [nzyxdim];
if (data == NULL)
REPORT_ERROR( "Allocate: No space left");
}
nzyxdimAlloc = nzyxdim;
}
/** Core allocate without dimensions.
*
* It is supposed the dimensions are set previously with setXdim(x), setYdim(y)
* setZdim(z), setNdim(n) or with setDimensions(Xdim, Ydim, Zdim, Ndim);
*
*/
void coreAllocateReuse()
{
if(data != NULL && nzyxdim <= nzyxdimAlloc)
return;
else if (nzyxdim > nzyxdimAlloc)
coreDeallocate();
if (nzyxdim < 0)
REPORT_ERROR("coreAllocateReuse:Cannot allocate a negative number of bytes");
if (mmapOn)
{
mapFile.initRandom(8);
mapFile = mapFile.addExtension("tmp");
if ( ( mFd = open(mapFile.c_str(), O_RDWR | O_CREAT | O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) ) == -1 )
REPORT_ERROR("MultidimArray::coreAllocateReuse: Error creating map file.");
if ((lseek(mFd, nzyxdim*sizeof(T), SEEK_SET) == -1) || (::write(mFd,"",1) == -1))// Use of :: to call write from global space due to confict with multidimarray::write
{
close(mFd);
REPORT_ERROR("MultidimArray::coreAllocateReuse: Error 'stretching' the map file.");
}
if ( (data = (T*) mmap(0,nzyxdim*sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0)) == (void*) -1 )
REPORT_ERROR("MultidimArray::coreAllocateReuse: mmap failed.");
}
else
{
data = new T [nzyxdim];
if (data == NULL)
REPORT_ERROR( "Allocate: No space left");
}
nzyxdimAlloc = nzyxdim;
}
/** Sets mmap.
*
* Sets on/off mmap flag to allocate memory in a file.
*
*/
void setMmap(bool mmap)
{
mmapOn = mmap;
}
/** Core deallocate.
* Free all data.
*/
void coreDeallocate()
{
if (data != NULL && destroyData)
{
if (mmapOn)
{
munmap(data,nzyxdimAlloc*sizeof(T));
close(mFd);
remove(mapFile.c_str());
}
else {
delete[] data;
}
}
data=NULL;
nzyxdimAlloc = 0;
}
/** Alias a multidimarray.
*
* Treat the multidimarray as if it were a volume. The data is not copied
* into new memory, but a pointer to the multidimarray is copied.
* You should not make any operation on this volume such that the
* memory locations are changed
*/
void alias(const MultidimArray<T> &m)
{
copyShape(m);
this->data=m.data;
this->destroyData=false;
}
//@}
/// @name Size
//@{
/** Sets new 4D dimensions.
*
* Note that the dataArray is NOT resized. This should be done separately with coreAllocate()
*
*/
void setDimensions(long int Xdim, long int Ydim, long int Zdim, long int Ndim)
{
ndim=Ndim;
zdim=Zdim;
ydim=Ydim;
xdim=Xdim;
yxdim=ydim*xdim;
zyxdim=zdim*yxdim;
nzyxdim=ndim*zyxdim;
}
/** Sets new N dimension.
*
* Note that the dataArray is NOT resized. This should be done separately with coreAllocate()
*
*/
void setNdim(long int Ndim)
{
ndim = Ndim;
nzyxdim=ndim*zyxdim;
}
/** Sets new Z dimension.
*
* Note that the dataArray is NOT resized. This should be done separately with coreAllocate()
*
*/
void setZdim(long int Zdim)
{
zdim = Zdim;
zyxdim=zdim*yxdim;
nzyxdim=ndim*zyxdim;
}
/** Sets new Y dimension.
*
* Note that the dataArray is NOT resized. This should be done separately with coreAllocate()
*
*/
void setYdim(long int Ydim)
{
ydim = Ydim;
yxdim=ydim*xdim;
zyxdim=zdim*yxdim;
nzyxdim=ndim*zyxdim;
}
/** Sets new X dimension.
*
* Note that the dataArray is NOT resized. This should be done separately with coreAllocate()
*
*/
void setXdim(long int Xdim)
{
xdim = Xdim;
yxdim=ydim*xdim;
zyxdim=zdim*yxdim;
nzyxdim=ndim*zyxdim;
}
/** Copy the shape parameters
*
*/
void copyShape(const MultidimArray<T> &m)
{
ndim=m.ndim;
zdim=m.zdim;
ydim=m.ydim;
xdim=m.xdim;
yxdim=m.yxdim;
zyxdim=m.zyxdim;
nzyxdim=m.nzyxdim;
zinit=m.zinit;
yinit=m.yinit;
xinit=m.xinit;
}
/** Resize to a given size
*
* This function resize the actual array to the given size. The origin is
* not modified. If the actual array is larger than the pattern then the
* values outside the new size are lost, if it is smaller then 0's are
* added. An exception is thrown if there is no memory.
*
* @code
* V1.resize(3, 3, 2);
* @endcode
*/
void resize(long int Ndim, long int Zdim, long int Ydim, long int Xdim)
{
if (Ndim*Zdim*Ydim*Xdim == nzyxdimAlloc && data != NULL)
return;
if (Xdim <= 0 || Ydim <= 0 || Zdim <= 0 || Ndim <= 0)
{
clear();
return;
}
// data can be NULL while xdim etc are set to non-zero values
// (This can happen for reading of images...)
// In that case, initialize data to zeros.
if (NZYXSIZE(*this) > 0 && data == NULL)
{
coreAllocate();
return;
}
// Ask for memory
size_t YXdim=Ydim*Xdim;
size_t ZYXdim=Zdim*YXdim;
size_t NZYXdim=Ndim*ZYXdim;
int new_mFd = 0;
FileName newMapFile;
T * new_data;
try
{
if (mmapOn)
{
newMapFile.initRandom(8);
newMapFile = newMapFile.addExtension("tmp");
if ( ( new_mFd = open(newMapFile.c_str(), O_RDWR | O_CREAT | O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) ) == -1 )
REPORT_ERROR("MultidimArray::resize: Error creating map file.");
if ((lseek(new_mFd, NZYXdim*sizeof(T)-1, SEEK_SET) == -1) || (::write(new_mFd,"",1) == -1))
{
close(new_mFd);
REPORT_ERROR("MultidimArray::resize: Error 'stretching' the map file.");
}
if ( (new_data = (T*) mmap(0,NZYXdim*sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, new_mFd, 0)) == (void*) -1 )
REPORT_ERROR("MultidimArray::resize: mmap failed.");
}
else
new_data = new T [NZYXdim];
}
catch (std::bad_alloc &)
{
REPORT_ERROR( "Allocate: No space left");
}
// Copy needed elements, fill with 0 if necessary
if ( (ZSIZE(*this)<= Zdim) && (YSIZE(*this)<= Ydim) && (XSIZE(*this)<= Xdim) ) {
for (long int l = 0; l < Ndim; l++)
for (long int k = 0; k < Zdim; k++)
for (long int i = 0; i < Ydim; i++) {
#pragma simd
for (long int j = 0; j < Xdim; j++)
{
new_data[l*ZYXdim + k*YXdim+i*Xdim+j] = 0;
}
}
for (long int l = 0; l < Ndim; l++)
for (long int k = 0; k < ZSIZE(*this); k++)
for (long int i = 0; i < YSIZE(*this); i++) {
#pragma simd
for (long int j = 0; j < XSIZE(*this); j++)
{
/*T val;
val = DIRECT_A3D_ELEM(*this, k, i, j);
new_data[l*ZYXdim + k*YXdim+i*Xdim+j] = val;*/
new_data[l*ZYXdim + k*YXdim+i*Xdim+j] = DIRECT_A3D_ELEM(*this, k, i, j);
}
}
} else {
for (long int l = 0; l < Ndim; l++)
for (long int k = 0; k < Zdim; k++)
for (long int i = 0; i < Ydim; i++)
for (long int j = 0; j < Xdim; j++)
{
T val;
if (k >= ZSIZE(*this))
val = 0;
else if (i >= YSIZE(*this))
val = 0;
else if (j >= XSIZE(*this))
val = 0;
else
val = DIRECT_A3D_ELEM(*this, k, i, j);
new_data[l*ZYXdim + k*YXdim+i*Xdim+j] = val;
}
}
// deallocate old vector
coreDeallocate();
// assign *this vector to the newly created
data = new_data;
ndim = Ndim;
xdim = Xdim;
ydim = Ydim;
zdim = Zdim;
yxdim = Ydim * Xdim;
zyxdim = Zdim * yxdim;
nzyxdim = Ndim * zyxdim;
mFd = new_mFd;
mapFile = newMapFile;
nzyxdimAlloc = nzyxdim;
}