Skip to content

Commit 2c19405

Browse files
authored
Merge pull request #112 from computationalmodelling/openmp
Speed Improvements
2 parents dd176a4 + 8dcc0cd commit 2c19405

30 files changed

+163
-154
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROJECT_DIR = $(abspath .)
22
EXTENSIONS_DIR = ${PROJECT_DIR}/fidimag/extensions
33
PYTHON = python3
4-
PYTEST = py.test-3
4+
PYTEST = ${PYTHON} -m pytest
55

66
#####################
77
# Cython Extensions #

fidimag/atomistic/lib/anis.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "clib.h"
22

33

4-
void compute_anis(double *spin, double *field, double *energy,
5-
double *Ku, double *axis, int n) {
4+
void compute_anis(double *restrict spin, double *restrict field, double *restrict energy,
5+
double *restrict Ku, double *restrict axis, int n) {
66

77
/* Remember that the magnetisation order is
88
* mx1, my1, mz1, mx2, my2, mz2, mx3,...
@@ -33,8 +33,8 @@ void compute_anis(double *spin, double *field, double *energy,
3333
}
3434

3535

36-
void compute_anis_cubic(double *spin, double *field, double *energy,
37-
double *Kc, int n) {
36+
void compute_anis_cubic(double *restrict spin, double *restrict field, double *restrict energy,
37+
double *restrict Kc, int n) {
3838

3939
/* Remember that the magnetisation order is
4040
* mx1, my1, mz1, mx2, my2, mz2, mx3,...

fidimag/atomistic/lib/clib.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,47 @@ inline double cross_z(double a0, double a1, double a2, double b0, double b1,
2929
// ----------------------------------------------------------------------------
3030
// From exch.c
3131

32-
void compute_exch_field(double *spin, double *field, double *energy, double Jx,
33-
double Jy, double Jz, int *ngbs, int n, int n_ngbs);
32+
void compute_exch_field(double *restrict spin, double *restrict field, double *restrict energy, double Jx,
33+
double Jy, double Jz, int *restrict ngbs, int n, int n_ngbs);
3434

35-
void compute_exch_field_spatial(double *spin, double *field, double *energy,
36-
double *J, int *ngbs, int n, int n_ngbs);
35+
void compute_exch_field_spatial(double *restrict spin, double *restrict field, double *restrict energy,
36+
double *restrict J, int *restrict ngbs, int n, int n_ngbs);
3737

38-
double compute_exch_energy(double *spin, double Jx, double Jy, double Jz,
38+
double compute_exch_energy(double *restrict spin, double Jx, double Jy, double Jz,
3939
int nx, int ny, int nz, int xperiodic,
4040
int yperiodic);
4141

42-
void compute_full_exch_field(double *spin, double *field, double *energy,
43-
double *J, int *ngbs, int n, int n_ngbs,
44-
int n_shells, int *n_ngbs_shell, int *sum_ngbs_shell
42+
void compute_full_exch_field(double *restrict spin, double *restrict field, double *restrict energy,
43+
double *restrict J, int *restrict ngbs, int n, int n_ngbs,
44+
int n_shells, int *restrict n_ngbs_shell, int *restrict sum_ngbs_shell
4545
);
4646

4747
// -----------------------------------------------------------------------------
4848
// From anis.c
4949

50-
void compute_anis(double *spin, double *field, double *energy, double *Ku,
51-
double *axis, int n);
52-
void compute_anis_cubic(double *spin, double *field, double *energy,
50+
void compute_anis(double *restrict spin, double *restrict field, double *restrict energy, double *restrict Ku,
51+
double *restrict axis, int n);
52+
void compute_anis_cubic(double *restrict spin, double *restrict field, double *restrict energy,
5353
double *Kc, int n);
5454

5555
// ----------------------------------------------------------------------------
5656
// From dmi.c
5757

58-
void dmi_field_bulk(double *spin, double *field, double *energy, double *D,
59-
int *ngbs, int n, int n_ngbs);
58+
void dmi_field_bulk(double *restrict spin, double *restrict field, double *restrict energy, double *D,
59+
int *restrict ngbs, int n, int n_ngbs);
6060

6161
void dmi_field_interfacial_atomistic(double *spin, double *field,
6262
double *energy, double D, int *ngbs, int n,
6363
int n_ngbs, int n_ngbs_dmi, double *DMI_vec);
6464

65-
double dmi_energy(double *spin, double D, int nx, int ny, int nz, int xperiodic,
65+
double dmi_energy(double *restrict spin, double D, int nx, int ny, int nz, int xperiodic,
6666
int yperiodic);
6767

6868
// ----------------------------------------------------------------------------
6969
// From demag_full.c
7070

71-
void demag_full(double *spin, double *field, double *energy, double *coords,
72-
double *mu_s, double *mu_s_scale, int n);
71+
void demag_full(double *restrict spin, double *restrict field, double *restrict energy, double *restrict coords,
72+
double *restrict mu_s, double *restrict mu_s_scale, int n);
7373

7474
// ----------------------------------------------------------------------------
7575
// From util.c
@@ -93,15 +93,15 @@ void compute_px_py_c(double *spin, int nx, int ny, int nz, double *px,
9393

9494
void normalise(double *m, int *pins, int n);
9595

96-
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
97-
double *mu_s_inv, int *pins, double *eta, int n, double gamma,
96+
void llg_rhs_dw_c(double *restrict m, double *restrict h, double *restrict dm, double *restrict T, double *restrict alpha,
97+
double *restrict mu_s_inv, int *pins, double *restrict eta, int n, double gamma,
9898
double dt);
9999

100100
// ----------------------------------------------------------------------------
101101
// From mc.c
102102

103-
void llg_s_rhs(double *dm_dt, double *spin, double *h, double *alpha,
104-
double *chi, double gamma, int n);
103+
void llg_s_rhs(double *restrict dm_dt, double *restrict spin, double *restrict h, double *restrict alpha,
104+
double *restrict chi, double gamma, int n);
105105

106106
void run_step_mc(mt19937_state *state, double *spin, double *new_spin,
107107
int *ngbs, int *nngbs, int n_ngbs, double J, double J1, double D,

fidimag/atomistic/lib/demag_full.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "math.h"
33
#include "stdlib.h"
44

5-
void demag_full(double *spin, double *field, double *energy, double *coords,
6-
double *mu_s, double *mu_s_scale, int n) {
5+
void demag_full(double *restrict spin, double *restrict field, double *restrict energy, double *restrict coords,
6+
double *restrict mu_s, double *restrict mu_s_scale, int n) {
77

88
/* Full calculation of the Demagnetising field for atomistic systems
99
* The main idea is to iterate through every lattice site, and sum

fidimag/atomistic/lib/dmi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ boundaries
2727
2828
*/
2929

30-
void dmi_field_bulk(double *spin, double *field,
31-
double *energy, double *_D, int *ngbs, int nxyz, int n_ngbs) {
30+
void dmi_field_bulk(double *restrict spin, double *restrict field,
31+
double *restrict energy, double *restrict _D, int *restrict ngbs, int nxyz, int n_ngbs) {
3232

3333
/* Bulk DMI field and energy computation
3434
*
@@ -112,8 +112,8 @@ void dmi_field_bulk(double *spin, double *field,
112112
}
113113
}
114114

115-
void dmi_field_interfacial_atomistic(double *spin, double *field, double *energy,
116-
double D, int *ngbs, int n, int n_ngbs, int n_ngbs_dmi, double *DMI_vec) {
115+
void dmi_field_interfacial_atomistic(double *restrict spin, double *restrict field, double *restrict energy,
116+
double D, int *restrict ngbs, int n, int n_ngbs, int n_ngbs_dmi, double *restrict DMI_vec) {
117117

118118
/* Interfacial DMI field and energy computation
119119
*
@@ -217,7 +217,7 @@ inline double single_energy_z(double D, double Si[3], double Sj[3]){
217217
return tz;
218218
}
219219

220-
double dmi_energy(double *spin, double D, int nx, int ny, int nz, int xperiodic, int yperiodic) {
220+
double dmi_energy(double *restrict spin, double D, int nx, int ny, int nz, int xperiodic, int yperiodic) {
221221

222222
int nyz = ny * nz;
223223
int n1 = nx * nyz, n2 = 2 * n1;

fidimag/atomistic/lib/exch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ The ngbs array also gives the correct indexes for the spins at periodic
3535
boundaries
3636
3737
*/
38-
void compute_exch_field(double *spin, double *field, double *energy,
38+
void compute_exch_field(double *restrict spin, double *restrict field, double *restrict energy,
3939
double Jx, double Jy, double Jz,
40-
int *ngbs, int n, int n_ngbs) {
40+
int *restrict ngbs, int n, int n_ngbs) {
4141

4242
#pragma omp parallel for
4343
for (int i = 0; i < n; i++) {
@@ -69,7 +69,7 @@ void compute_exch_field(double *spin, double *field, double *energy,
6969

7070

7171

72-
double compute_exch_energy(double *spin, double Jx, double Jy, double Jz,
72+
double compute_exch_energy(double *restrict spin, double Jx, double Jy, double Jz,
7373
int nx, int ny, int nz, int xperiodic, int yperiodic) {
7474

7575
int nyz = ny * nz;
@@ -137,8 +137,8 @@ double compute_exch_energy(double *spin, double Jx, double Jy, double Jz,
137137
Note that the pair <i,j> only run once for each pair.
138138
139139
*/
140-
void compute_exch_field_spatial(double *spin, double *field, double *energy,
141-
double *J, int *ngbs, int n, int n_ngbs) {
140+
void compute_exch_field_spatial(double *restrict spin, double *restrict field, double *restrict energy,
141+
double *restrict J, int *restrict ngbs, int n, int n_ngbs) {
142142

143143
#pragma omp parallel for
144144
for (int i = 0; i < n; i++) {
@@ -187,9 +187,9 @@ void compute_exch_field_spatial(double *spin, double *field, double *energy,
187187
* Thus, we can locate ngbs from cols 0-5, 6-11, 12, 23, ... etc
188188
*
189189
*/
190-
void compute_full_exch_field(double *spin, double *field, double *energy,
190+
void compute_full_exch_field(double *restrict spin, double *restrict field, double *restrict energy,
191191
double J[9], int *ngbs, int n, int n_ngbs,
192-
int n_shells, int *n_ngbs_shell, int *sum_ngbs_shell
192+
int n_shells, int *restrict n_ngbs_shell, int *restrict sum_ngbs_shell
193193
) {
194194

195195
#pragma omp parallel for

fidimag/atomistic/lib/mc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ inline double cubic_energy_site(double *m, double Kc) {
7575
return -Kc * (mx2 * mx2 + my2 * my2 + mz2 * mz2);
7676
}
7777

78-
double compute_deltaE_anisotropy(double *spin, double *new_spin, double *h,
78+
double compute_deltaE_anisotropy(double *restrict spin, double *restrict new_spin, double *restrict h,
7979
double Kc, int i, int new_i) {
8080

8181
double energy1 = -dot(&spin[3 * i], &h[3 * i]); // zeeman energy

fidimag/atomistic/lib/sllg.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* n is the spin number
55
* eta is the random number array
66
*/
7-
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha, double *mu_s_inv, int *pins, double *eta, int n, double gamma, double dt) {
7+
void llg_rhs_dw_c(double *restrict m, double *restrict h, double *restrict dm, double *restrict T, double *restrict alpha,
8+
double *restrict mu_s_inv, int *restrict pins, double *restrict eta, int n, double gamma, double dt) {
89

910
double k_B = 1.3806505e-23;
1011
double Q = 2 * k_B * dt / gamma;
@@ -41,7 +42,7 @@ void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha, do
4142
}
4243
}
4344

44-
void normalise(double *m, int *pins, int n){
45+
void normalise(double *restrict m, int *restrict pins, int n){
4546
int i, j, k;
4647
double mm;
4748
for (int id = 0; id < n; id++) {

fidimag/atomistic/lib/util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ inline double volume(double S[3], double Si[3], double Sj[3]) {
1010
return tx + ty + tz;
1111
}
1212

13-
double skyrmion_number(double *spin, double *charge, int nx, int ny, int nz,
14-
int *ngbs, int n_ngbs) {
13+
double skyrmion_number(double *restrict spin, double *restrict charge, int nx, int ny, int nz,
14+
int *restrict ngbs, int n_ngbs) {
1515

1616
/* Calculation of the "Skyrmion number" Q for a two dimensional discrete
1717
* spin lattice in the x-y plane (also known

fidimag/common/dipolar/demag.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void compute_demag_tensors(fft_demag_plan *plan) {
141141

142142

143143
//used for debug
144-
void print_r(char *str, double *x, int n) {
144+
void print_r(char *str, double *restrict x, int n) {
145145
int i;
146146
printf("%s:\n", str);
147147
for (i = 0; i < n; i++) {
@@ -151,7 +151,7 @@ void print_r(char *str, double *x, int n) {
151151

152152
}
153153

154-
void print_c(char *str, fftw_complex *x, int n) {
154+
void print_c(char *str, fftw_complex *restrict x, int n) {
155155
int i;
156156
printf("%s\n", str);
157157
for (i = 0; i < n; i++) {
@@ -168,7 +168,7 @@ fft_demag_plan *create_plan(void) {
168168
return plan;
169169
}
170170

171-
void init_plan(fft_demag_plan *plan, double dx, double dy,
171+
void init_plan(fft_demag_plan *restrict plan, double dx, double dy,
172172
double dz, int nx, int ny, int nz) {
173173

174174
//plan->mu_s = mu_s;
@@ -225,7 +225,7 @@ void init_plan(fft_demag_plan *plan, double dx, double dy,
225225
}
226226

227227

228-
void create_fftw_plan(fft_demag_plan *plan) {
228+
void create_fftw_plan(fft_demag_plan *restrict plan) {
229229

230230

231231
plan->tensor_plan = fftw_plan_dft_r2c_3d(plan->lenz, plan->leny,
@@ -272,7 +272,7 @@ void create_fftw_plan(fft_demag_plan *plan) {
272272

273273
//The computed results doesn't consider the coefficient of \frac{\mu_0}{4 \pi}, the
274274
//reason is in future we can use the following code directly for continuum case
275-
void compute_fields(fft_demag_plan *plan, double *spin, double *mu_s, double *field) {
275+
void compute_fields(fft_demag_plan *restrict plan, double *restrict spin, double *restrict mu_s, double *restrict field) {
276276

277277
int i, j, k, id1, id2;
278278

@@ -362,7 +362,7 @@ void compute_fields(fft_demag_plan *plan, double *spin, double *mu_s, double *fi
362362
}
363363

364364
//only used for debug
365-
void exact_compute(fft_demag_plan *plan, double *spin, double *mu_s, double *field) {
365+
void exact_compute(fft_demag_plan *restrict plan, double *restrict spin, double *restrict mu_s, double *restrict field) {
366366
int i, j, k, index;
367367
int ip, jp, kp, idf, ids;
368368
int nx = plan->nx;
@@ -417,7 +417,7 @@ void exact_compute(fft_demag_plan *plan, double *spin, double *mu_s, double *fi
417417

418418
}
419419

420-
double compute_demag_energy(fft_demag_plan *plan, double *spin, double *mu_s, double *field) {
420+
double compute_demag_energy(fft_demag_plan *restrict plan, double *restrict spin, double *restrict mu_s, double *restrict field) {
421421

422422
int i,j;
423423

@@ -436,7 +436,7 @@ double compute_demag_energy(fft_demag_plan *plan, double *spin, double *mu_s, do
436436

437437
}
438438

439-
void finalize_plan(fft_demag_plan *plan) {
439+
void finalize_plan(fft_demag_plan *restrict plan) {
440440

441441
fftw_destroy_plan(plan->m_plan);
442442
fftw_destroy_plan(plan->h_plan);

0 commit comments

Comments
 (0)