11#include " finufft/finufft_utils.hpp"
22#include < finufft/test_defs.h>
3+ #include < memory>
34
45// for sleep call
56#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
@@ -93,58 +94,73 @@ int main(int argc, char *argv[])
9394 N3 = (N3 == 0 ) ? 1 : N3;
9495 BIGINT N = N1 * N2 * N3;
9596
96- FLT *s = nullptr ;
97- FLT *t = nullptr ;
98- FLT *u = nullptr ;
97+ std::unique_ptr< FLT[]> s ;
98+ std::unique_ptr< FLT[]> t ;
99+ std::unique_ptr< FLT[]> u ;
99100 if (type == 3 ) { // make target freq NU pts for type 3 (N of them)...
100- s = static_cast <FLT *>(std::malloc (sizeof (FLT) * N)); // targ freqs (1-cmpt)
101- FLT S1 = (FLT)N1 / 2 ;
101+ s = std::make_unique<FLT[]>(N); // targ freqs (1-cmpt)
102+ FLT *s_ptr = s.get ();
103+ FLT S1 = (FLT)N1 / 2 ;
102104#pragma omp parallel
103105 {
104106 unsigned int se = MY_OMP_GET_THREAD_NUM (); // needed for parallel random #s
105107#pragma omp for schedule(dynamic, TEST_RANDCHUNK)
106108 for (BIGINT k = 0 ; k < N; ++k) {
107- s [k] = S1 * (1.7 + randm11r (&se)); // note the offset, to test type 3.
109+ s_ptr [k] = S1 * (1.7 + randm11r (&se)); // note the offset, to test type 3.
108110 }
109- if (ndim > 1 ) {
110- t = static_cast <FLT *>(std::malloc (sizeof (FLT) * N)); // targ freqs (2-cmpt)
111- FLT S2 = (FLT)N2 / 2 ;
111+ }
112+ if (ndim > 1 ) {
113+ t = std::make_unique<FLT[]>(N); // targ freqs (2-cmpt)
114+ FLT *t_ptr = t.get ();
115+ FLT S2 = (FLT)N2 / 2 ;
116+ #pragma omp parallel
117+ {
118+ unsigned int se = MY_OMP_GET_THREAD_NUM (); // needed for parallel random #s
112119#pragma omp for schedule(dynamic, TEST_RANDCHUNK)
113120 for (BIGINT k = 0 ; k < N; ++k) {
114- t [k] = S2 * (-0.5 + randm11r (&se));
121+ t_ptr [k] = S2 * (-0.5 + randm11r (&se));
115122 }
116123 }
117- if (ndim > 2 ) {
118- u = static_cast <FLT *>(std::malloc (sizeof (FLT) * N)); // targ freqs (3-cmpt)
119- FLT S3 = (FLT)N3 / 2 ;
124+ }
125+ if (ndim > 2 ) {
126+ u = std::make_unique<FLT[]>(N); // targ freqs (3-cmpt)
127+ FLT *u_ptr = u.get ();
128+ FLT S3 = (FLT)N3 / 2 ;
129+ #pragma omp parallel
130+ {
131+ unsigned int se = MY_OMP_GET_THREAD_NUM (); // needed for parallel random #s
120132#pragma omp for schedule(dynamic, TEST_RANDCHUNK)
121133 for (BIGINT k = 0 ; k < N; ++k) {
122- u [k] = S3 * (0.9 + randm11r (&se));
134+ u_ptr [k] = S3 * (0.9 + randm11r (&se));
123135 }
124136 }
125137 }
126138 }
127139
128- CPX *c = static_cast <CPX *>(std::malloc (sizeof (CPX) * M * ntransf)); // strengths
129- CPX *F = static_cast <CPX *>(std::malloc (sizeof (CPX) * N * ntransf)); // mode ampls
130-
131- FLT *x = static_cast <FLT *>(std::malloc (sizeof (FLT) * M)),
132- *y = nullptr ,
133- *z = nullptr ; // NU pts x coords
134- if (ndim > 1 ) y = static_cast <FLT *>(std::malloc (sizeof (FLT) * M)); // NU pts y coords
135- if (ndim > 2 ) z = static_cast <FLT *>(std::malloc (sizeof (FLT) * M)); // NU pts z coords
140+ auto c = std::make_unique<CPX[]>(M * ntransf); // strengths
141+ auto F = std::make_unique<CPX[]>(N * ntransf); // mode ampls
142+
143+ auto x = std::make_unique<FLT[]>(M);
144+ std::unique_ptr<FLT[]> y;
145+ std::unique_ptr<FLT[]> z; // NU pts coords
146+ if (ndim > 1 ) y = std::make_unique<FLT[]>(M); // NU pts y coords
147+ if (ndim > 2 ) z = std::make_unique<FLT[]>(M); // NU pts z coords
148+ FLT *x_ptr = x.get ();
149+ FLT *y_ptr = y.get ();
150+ FLT *z_ptr = z.get ();
151+ CPX *c_ptr = c.get ();
136152#pragma omp parallel
137153 {
138154 unsigned int se = MY_OMP_GET_THREAD_NUM (); // needed for parallel random #s
139155#pragma omp for schedule(dynamic, TEST_RANDCHUNK)
140156 for (BIGINT j = 0 ; j < M; ++j) {
141- x [j] = PI * randm11r (&se);
142- if (y) y [j] = PI * randm11r (&se);
143- if (z) z [j] = PI * randm11r (&se);
157+ x_ptr [j] = PI * randm11r (&se);
158+ if (y_ptr) y_ptr [j] = PI * randm11r (&se);
159+ if (z_ptr) z_ptr [j] = PI * randm11r (&se);
144160 }
145161#pragma omp for schedule(dynamic, TEST_RANDCHUNK)
146162 for (BIGINT i = 0 ; i < ntransf * M; i++) // random strengths
147- c [i] = crandm11r (&se);
163+ c_ptr [i] = crandm11r (&se);
148164 }
149165
150166 // Andrea found the following are needed to get reliable independent timings:
@@ -177,7 +193,15 @@ int main(int argc, char *argv[])
177193 }
178194
179195 timer.restart (); // Guru Step 2
180- ier = FINUFFT_SETPTS (plan, M, x, y, z, N, s, t, u); // (t1,2: N,s,t,u ignored)
196+ ier = FINUFFT_SETPTS (plan,
197+ M,
198+ x.get (),
199+ y.get (),
200+ z.get (),
201+ N,
202+ s.get (),
203+ t.get (),
204+ u.get ()); // (t1,2: N,s,t,u ignored)
181205 double sort_t = timer.elapsedsec ();
182206 if (ier) {
183207 std::printf (" error (ier=%d)!\n " , ier);
@@ -193,7 +217,7 @@ int main(int argc, char *argv[])
193217 }
194218
195219 timer.restart (); // Guru Step 3
196- ier = FINUFFT_EXECUTE (plan, c, F);
220+ ier = FINUFFT_EXECUTE (plan, c. get () , F. get () );
197221 double exec_t = timer.elapsedsec ();
198222 if (ier) {
199223 std::printf (" error (ier=%d)!\n " , ier);
@@ -235,7 +259,8 @@ int main(int argc, char *argv[])
235259 // this used to actually call Alex's old (v1.1) src/finufft?d.cpp routines.
236260 // Since we don't want to ship those, we now call the simple interfaces.
237261
238- double simpleTime = many_simple_calls (c, F, x, y, z, plan);
262+ double simpleTime =
263+ many_simple_calls (c.get (), F.get (), x.get (), y.get (), z.get (), plan);
239264 if (std::isnan (simpleTime)) return 1 ;
240265
241266 if (type != 3 )
@@ -263,15 +288,6 @@ int main(int argc, char *argv[])
263288 // (must be done *after* many_simple_calls, which sneaks a look at the plan!)
264289 // however, segfaults, maybe because plan->opts.debug changed?
265290
266- // ---------------------------- Free Memory (no need to test if NULL)
267- std::free (F);
268- std::free (c);
269- std::free (x);
270- std::free (y);
271- std::free (z);
272- std::free (s);
273- std::free (t);
274- std::free (u);
275291 return 0 ;
276292}
277293
0 commit comments