-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuildMagTenseMEX.m
178 lines (163 loc) · 6.97 KB
/
buildMagTenseMEX.m
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
function buildMagTenseMEX(USE_RELEASE, USE_CUDA, USE_CVODE)
%use clear all as this also clears dependencies to the .mex files and thus they can be overwritten
arguments
USE_RELEASE {mustBeNumericOrLogical} = true;
USE_CUDA {mustBeNumericOrLogical} = true;
USE_CVODE {mustBeNumericOrLogical} = true;
end
pause_time = 1; %Time to wait between making and moving the generated files
mex_root = '../source/MagTenseMEX/MagTenseMEX/';
NumericalIntegration_path = '../source/NumericalIntegration/NumericalIntegration';
DemagField_path = '../source/DemagField/DemagField';
TileDemagTensor_path = '../source/TileDemagTensor/TileDemagTensor';
MagTenseMicroMag_path = '../source/MagTenseMicroMag';
ForceIntegrator_path = '../source/MagneticForceIntegrator/MagneticForceIntegrator';
FortranCuda_path = '../source/MagTenseFortranCuda/cuda';
if (ispc)
VS_STUDIO = true;
MKL_STATIC = true;
mkl_include = '"C:\Program Files (x86)\Intel\oneAPI\mkl\latest\include"';
mkl_lp64 = '"C:\Program Files (x86)\Intel\oneAPI\mkl\latest\include\intel64\lp64"';
mkl_lib = '"C:\Program Files (x86)\Intel\oneAPI\mkl\latest\lib\intel64"';
cuda_root = '"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\lib\x64"';
cvode_include = '"C:\Program Files (x86)\sundials-4.1.0\instdir\fortran"';
cvode_lib = '"C:\Program Files (x86)\sundials-4.1.0\instdir\lib"';
mex_suffix = 'w';
else
VS_STUDIO = false;
MKL_STATIC = false;
compiler_root = '/opt/intel/oneapi/compiler/latest';
mkl_root = '/opt/intel/oneapi/mkl/latest';
mkl_lib = '/opt/intel/oneapi/mkl/latest/lib/intel64';
cuda_root = '/usr/local/cuda-12.3/lib64/';
cvode_include = '/usr/local/sundials-4.1.0/instdir/fortran';
cvode_lib = '/usr/local/sundials-4.1.0/instdir/lib';
mex_suffix = 'a';
end
if (USE_RELEASE)
DEBUG = '';
BUILD = '/x64/Release';
else
DEBUG = '-g';
BUILD = '/x64/Debug';
end
if (USE_CUDA)
CUDA = ['-L' cuda_root ' -lcublas -lcudart -lcuda -lcusparse'];
if (ispc)
OBJS = ['OBJS="$OBJS ' FortranCuda_path '/MagTenseCudaBlasICLWrapper.obj ' FortranCuda_path '/MagTenseCudaBlas.obj" '];
else
OBJS = ['OBJS="$OBJS ' FortranCuda_path '/MagTenseCudaBlasICLWrapper.o ' FortranCuda_path '/MagTenseCudaBlas.o" '];
end
BUILD_MagTenseMicroMag = BUILD;
else
CUDA = '';
OBJS = '';
BUILD_MagTenseMicroMag = [BUILD '_no_CUDA'];
end
if (USE_CVODE)
CVODE_include = join(['-I' cvode_include], '');
CVODE = ['-L' cvode_lib ' -lsundials_nvecserial -lsundials_sunmatrixdense -lsundials_sunlinsoldense' ...
' -lsundials_fnvecserial_mod -lsundials_cvode -lsundials_fsunnonlinsolfixedpoint_mod'];
else
CVODE_include = '';
CVODE = '';
BUILD = [BUILD '_no_CVODE'];
BUILD_MagTenseMicroMag = [BUILD_MagTenseMicroMag '_no_CVODE'];
end
if (VS_STUDIO)
BUILD = [BUILD ' '];
BUILD_MagTenseMicroMag = [BUILD_MagTenseMicroMag ' '];
else
BUILD = ' ';
BUILD_MagTenseMicroMag = ' ';
end
if (ispc)
DEFINES = '-R2018a';
FFLAGS = 'COMPFLAGS="$COMPFLAGS /free /nologo /real-size:64 /O2 /assume:nocc_omp /Qopenmp /fpp /fpe:0 /fp:source /fp:precise"';
if (USE_CUDA)
FFLAGS = [FFLAGS(1:(end-1)) ' /libs:static"'];
end
INCLUDE = ['-I' mkl_include ' -I' mkl_lp64 ' -I' NumericalIntegration_path BUILD '-I' DemagField_path BUILD '-I' TileDemagTensor_path ...
BUILD '-I' MagTenseMicroMag_path BUILD_MagTenseMicroMag '-I' ForceIntegrator_path BUILD CVODE_include];
LIBS = ['-L' MagTenseMicroMag_path BUILD_MagTenseMicroMag '-lMagTenseMicroMag -L' DemagField_path BUILD ...
' -lDemagField -L' TileDemagTensor_path BUILD ' -lTileDemagTensor -L' NumericalIntegration_path BUILD ...
' -lNumericalIntegration -L' ForceIntegrator_path BUILD ' -lMagneticForceIntegrator'];
if (MKL_STATIC)
MKL = ['-L' mkl_lib ' -lmkl_intel_thread -lmkl_core -lmkl_intel_lp64 -lmkl_blas95_lp64 -llibiomp5md'];
else
MKL = ['-L' mkl_lib ' -lmkl_rt -lmkl_blas95_lp64'];
end
else
DEFINES = ['FC="' compiler_root '/bin/ifort" DEFINES="-DMATLAB_DEFAULT_RELEASE=R2018a"'];
INCLUDE = ['INCLUDE="$INCLUDE -I' mkl_root '/include -I' NumericalIntegration_path ' -I' DemagField_path ...
' -I' TileDemagTensor_path ' -I' MagTenseMicroMag_path ' -I' ForceIntegrator_path ' -I' mkl_root];
LIBS = ['-L' MagTenseMicroMag_path ' -lMagTenseMicroMag -L' DemagField_path ' -lDemagField -L' ...
TileDemagTensor_path ' -lTileDemagTensor -L' NumericalIntegration_path ' -lNumericalIntegration -L' ...
ForceIntegrator_path ' -lMagneticForceIntegrator'];
FFLAGS = 'FFLAGS="';
if (MKL_STATIC)
MKL = ['-liomp5 -lpthread -lm -ldl LINKLIBS="$LINKLIBS ' mkl_lib '/libmkl_intel_thread.a ' mkl_lib ...
'/libmkl_core.a ' mkl_lib '/libmkl_blas95_lp64.a ' mkl_lib '/libmkl_intel_lp64.a"'];
INCLUDE = [INCLUDE '/include/intel64/lp64 '];
else
MKL = ['-L' mkl_root '/lib/intel64 -lmkl_rt -lpthread -lm -ldl '];
if (USE_CUDA)
INCLUDE = [INCLUDE '/include/intel64/lp64 '];
MKL = [MKL '-lmkl_blas95_lp64'];
else
FFLAGS = [FFLAGS '-i8 '];
INCLUDE = [INCLUDE '/include/intel64/ilp64 '];
MKL = [MKL '-lmkl_blas95_ilp64'];
end
end
INCLUDE = [INCLUDE CVODE_include '"'];
FFLAGS = [FFLAGS '-r8 -O3 -assume nocc_omp -qopenmp -fpp -fpe0 -fp-model source -fp-model precise -fpic -diag-disable 10006 -libs:static"'];
end
%%------------------------------------------------------------------
%%--------------- Build the MEX files ------------------------------
%%----------------------------------- ------------------------------
names = ["MagTenseLandauLifshitzSolver", "IterateMagnetization", "getHFromTiles", "getNFromTile", "getMagForce"];
if (~USE_CUDA)
names(1) = "MagTenseLandauLifshitzSolverNoCUDA";
end
for i = 1:length(names)
if names(i) == "MagTenseLandauLifshitzSolverNoCUDA"
source = [mex_root "MagTenseLandauLifshitzSolver_mex.f90"];
orig_name = "MagTenseLandauLifshitzSolver";
else
source = [mex_root names(i) '_mex.f90'];
orig_name = names(i);
end
mex_str = ['mex' DEBUG DEFINES FFLAGS INCLUDE OBJS LIBS MKL CUDA CVODE join(source, '')];
disp(join(mex_str, ' '))
eval_MEX(join(mex_str, ' '))
pause(pause_time)
movefile(join([orig_name '_mex.mex' mex_suffix '64'], ''), join(['MEX_files/' names(i) '_mex.mex' mex_suffix '64'], ''));
end
end
function eval_MEX(mex_str)
try
eval(mex_str);
catch ME
if (length(ME.message(:)) > 117)
if (strcmp(ME.message(91:117),'mt : general error c101008d'))
fail_mex = true;
while fail_mex
try
disp('Microsoft manifest tool error - retrying')
eval(mex_str);
fail_mex = false;
catch
continue
end
end
else
disp(ME.message)
rethrow(ME)
end
else
disp(ME.message)
rethrow(ME)
end
end
end