Skip to content

Commit 8c6eb8c

Browse files
committed
test/example: move sizeof() to front in calls to malloc
1 parent f9d5e43 commit 8c6eb8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+204
-204
lines changed

examples/C/block_cyclic.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ int main(int argc, char** argv) {
185185

186186
/* First, fill the entire array with zeros, using a blocking I/O.
187187
Every process writes a subarray of size NY * myNX */
188-
buf = (int**) malloc(myNX * sizeof(int*));
188+
buf = (int**) malloc(sizeof(int*) * myNX);
189189
buf[0] = (int*) calloc(NY * myNX, sizeof(int));
190190
start[0] = 0; start[1] = myOff;
191191
count[0] = NY; count[1] = myNX;
@@ -197,12 +197,12 @@ int main(int argc, char** argv) {
197197
/* initialize the buffer with rank ID. Also make the case interesting,
198198
by allocating buffers separately */
199199
for (i=0; i<myNX; i++) {
200-
buf[i] = (int*) malloc(NY * sizeof(int));
200+
buf[i] = (int*) malloc(sizeof(int) * NY);
201201
for (j=0; j<NY; j++) buf[i][j] = rank+10;
202202
}
203203

204-
reqs = (int*) malloc(myNX * sizeof(int));
205-
sts = (int*) malloc(myNX * sizeof(int));
204+
reqs = (int*) malloc(sizeof(int) * myNX);
205+
sts = (int*) malloc(sizeof(int) * myNX);
206206

207207
/* each proc writes myNX columns of the 2D array, block_len controls the
208208
number of contiguous columns at a time */
@@ -272,15 +272,15 @@ int main(int argc, char** argv) {
272272
ERR
273273

274274
/* initialize the buffer with -1, so a read error can be pinpointed */
275-
buf = (int**) malloc(myNX * sizeof(int*));
276-
buf[0] = (int*) malloc(global_ny * myNX * sizeof(int));
275+
buf = (int**) malloc(sizeof(int*) * myNX);
276+
buf[0] = (int*) malloc(sizeof(int) * global_ny * myNX);
277277
for (i=0; i<myNX; i++) {
278278
if (i > 0) buf[i] = buf[i-1] + global_ny;
279279
for (j=0; j<global_ny; j++) buf[i][j] = -1;
280280
}
281281

282-
reqs = (int*) malloc(myNX * sizeof(int));
283-
sts = (int*) malloc(myNX * sizeof(int));
282+
reqs = (int*) malloc(sizeof(int) * myNX);
283+
sts = (int*) malloc(sizeof(int) * myNX);
284284

285285
/* each proc reads myNX columns of the 2D array, block_len controls the
286286
number of contiguous columns at a time */

examples/C/bput_varn_int64.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ static MPI_Offset *** calloc_3D(size_t z, size_t y, size_t x)
153153
{
154154
if (z*y*x == 0) return NULL;
155155
int _j, _k;
156-
MPI_Offset ***buf = (MPI_Offset***) malloc(z * sizeof(MPI_Offset**));
157-
MPI_Offset **bufy = (MPI_Offset**) malloc(z*y * sizeof(MPI_Offset*));
156+
MPI_Offset ***buf = (MPI_Offset***) malloc(sizeof(MPI_Offset**) * z);
157+
MPI_Offset **bufy = (MPI_Offset**) malloc(sizeof(MPI_Offset*) * z*y);
158158
MPI_Offset *bufx = (MPI_Offset*) calloc(z*y*x, sizeof(MPI_Offset));
159159
for (_k=0; _k<z; _k++, bufy+=y) {
160160
buf[_k] = bufy;
@@ -191,7 +191,7 @@ static int check_contents(int ncid, int *varid)
191191
3, 2, 0, 2, 3, 2, 3, 2, 1, 1,
192192
3, 0, 2, 1, 2, 0, 2, 1, 2, 0}};
193193

194-
long long *r_buffer = (long long*) malloc(NY*NX*sizeof(long long));
194+
long long *r_buffer = (long long*) malloc(sizeof(long long)*NY*NX);
195195

196196
for (i=0; i<4; i++) {
197197
for (j=0; j<NY*NX; j++) r_buffer[j] = 99999;
@@ -388,7 +388,7 @@ int main(int argc, char** argv)
388388
if (verbose) printf("req_lens[%d]=%d\n",i,req_lens[i]);
389389

390390
/* allocate I/O buffer and initialize its contents */
391-
buffer[i] = (long long*) malloc(req_lens[i] * sizeof(long long));
391+
buffer[i] = (long long*) malloc(sizeof(long long) * req_lens[i]);
392392
for (j=0; j<req_lens[i]; j++) buffer[i][j] = rank;
393393

394394
bufsize += req_lens[i];
@@ -420,7 +420,7 @@ int main(int argc, char** argv)
420420
MPI_Datatype buftype;
421421
MPI_Type_vector(req_lens[i], 1, 2, MPI_UNSIGNED, &buftype);
422422
MPI_Type_commit(&buftype);
423-
buffer[i] = (long long*) malloc(req_lens[i] * 2 * sizeof(long long));
423+
buffer[i] = (long long*) malloc(sizeof(long long) * req_lens[i] * 2);
424424
for (j=0; j<req_lens[i]*2; j++) buffer[i][j] = rank;
425425

426426
err = ncmpi_bput_varn(ncid, varid[i], num_segs[i], starts[i],

examples/C/bput_varn_uint.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ static MPI_Offset *** calloc_3D(size_t z, size_t y, size_t x)
129129
{
130130
if (z*y*x == 0) return NULL;
131131
int _j, _k;
132-
MPI_Offset ***buf = (MPI_Offset***) malloc(z * sizeof(MPI_Offset**));
133-
MPI_Offset **bufy = (MPI_Offset**) malloc(z*y * sizeof(MPI_Offset*));
132+
MPI_Offset ***buf = (MPI_Offset***) malloc(sizeof(MPI_Offset**) * z);
133+
MPI_Offset **bufy = (MPI_Offset**) malloc(sizeof(MPI_Offset*) * z*y);
134134
MPI_Offset *bufx = (MPI_Offset*) calloc(z*y*x, sizeof(MPI_Offset));
135135
for (_k=0; _k<z; _k++, bufy+=y) {
136136
buf[_k] = bufy;
@@ -167,7 +167,7 @@ static int check_contents(int ncid, int *varid)
167167
2, 2, 1, 0, 0, 0, 3, 3, 2, 2,
168168
3, 3, 3, 1, 2, 2, 2, 0, 0, 0}};
169169

170-
unsigned int *r_buffer = (unsigned int*) malloc(NY*NX*sizeof(unsigned int));
170+
unsigned int *r_buffer = (unsigned int*) malloc(sizeof(unsigned int)*NY*NX);
171171

172172
for (i=0; i<4; i++) {
173173
for (j=0; j<NY*NX; j++) r_buffer[j] = 99999;
@@ -322,7 +322,7 @@ int main(int argc, char** argv)
322322
if (verbose) printf("req_lens[%d]=%d\n",i,req_lens[i]);
323323

324324
/* allocate I/O buffer and initialize its contents */
325-
buffer[i] = (unsigned int*) malloc(req_lens[i] * sizeof(unsigned int));
325+
buffer[i] = (unsigned int*) malloc(sizeof(unsigned int) * req_lens[i]);
326326
for (j=0; j<req_lens[i]; j++) buffer[i][j] = rank;
327327

328328
bufsize += req_lens[i];
@@ -354,7 +354,7 @@ int main(int argc, char** argv)
354354
MPI_Datatype buftype;
355355
MPI_Type_vector(req_lens[i], 1, 2, MPI_UNSIGNED, &buftype);
356356
MPI_Type_commit(&buftype);
357-
buffer[i] = (unsigned int*) malloc(req_lens[i] * 2 * sizeof(unsigned int));
357+
buffer[i] = (unsigned int*) malloc(sizeof(unsigned int) * req_lens[i] * 2);
358358
for (j=0; j<req_lens[i]*2; j++) buffer[i][j] = rank;
359359

360360
err = ncmpi_bput_varn(ncid, varid[i], num_segs[i], starts[i],

examples/C/collective_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pnetcdf_io(MPI_Comm comm, char *filename, int cmode, int len)
154154

155155
/* allocate buffer and initialize with non-zero numbers */
156156
for (i=0; i<NUM_VARS; i++) {
157-
buf[i] = (int *) malloc(bufsize * sizeof(int));
157+
buf[i] = (int *) malloc(sizeof(int) * bufsize);
158158
for (j=0; j<bufsize; j++) buf[i][j] = rank * i + 123 + j;
159159
}
160160

examples/C/column_wise.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ int main(int argc, char** argv)
175175

176176
/* First, fill the entire array with zeros, using a blocking I/O.
177177
Every process writes a subarray of size NY * myNX */
178-
buf = (int**) malloc(myNX * sizeof(int*));
178+
buf = (int**) malloc(sizeof(int*) * myNX);
179179
buf[0] = (int*) calloc(NY * myNX, sizeof(int));
180180
start[0] = 0; start[1] = myOff;
181181
count[0] = NY; count[1] = myNX;
@@ -187,12 +187,12 @@ int main(int argc, char** argv)
187187
/* initialize the buffer with rank ID. Also make the case interesting,
188188
by allocating buffers separately */
189189
for (i=0; i<myNX; i++) {
190-
buf[i] = (int*) malloc(NY * sizeof(int));
190+
buf[i] = (int*) malloc(sizeof(int) * NY);
191191
for (j=0; j<NY; j++) buf[i][j] = rank;
192192
}
193193

194-
reqs = (int*) malloc(myNX * sizeof(int));
195-
sts = (int*) malloc(myNX * sizeof(int));
194+
reqs = (int*) malloc(sizeof(int) * myNX);
195+
sts = (int*) malloc(sizeof(int) * myNX);
196196

197197
/* each proc writes myNX single columns of the 2D array */
198198
start[0] = 0; start[1] = rank;

examples/C/flexible_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int main(int argc, char** argv)
189189
MPI_Type_commit(&subarray);
190190

191191
int buffer_len = (NZ+2*ghost_len) * (NY+2*ghost_len);
192-
buf_zy = (int*) malloc(buffer_len * sizeof(int));
192+
buf_zy = (int*) malloc(sizeof(int) * buffer_len);
193193
for (i=0; i<buffer_len; i++) buf_zy[i] = rank;
194194

195195
start[0] = NZ * rank; start[1] = 0;
@@ -243,7 +243,7 @@ int main(int argc, char** argv)
243243
MPI_Type_commit(&subarray);
244244

245245
buffer_len = (NY+2*ghost_len) * (NX+2*ghost_len);
246-
buf_yx = (double*) malloc(buffer_len * sizeof(double));
246+
buf_yx = (double*) malloc(sizeof(double) * buffer_len);
247247
for (i=0; i<buffer_len; i++) buf_yx[i] = rank;
248248

249249
start[0] = 0; start[1] = NX * rank;

examples/C/get_vara.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ pnetcdf_io(MPI_Comm comm, char *filename)
154154
err = ncmpi_inq_attlen(ncid, varid, "float_att_name", &len); ERR
155155

156156
/* get attribute contents */
157-
float_att = (float*) malloc(len * sizeof(float));
157+
float_att = (float*) malloc(sizeof(float) * len);
158158
err = ncmpi_get_att_float(ncid, varid, "float_att_name", float_att); ERR
159159
free(float_att);
160160

161161
/* the local array size */
162162
local_ny = global_ny;
163163
local_nx = global_nx / nprocs;
164-
buf = (int*) malloc(local_nx * local_ny * sizeof(int));
164+
buf = (int*) malloc(sizeof(int) * local_nx * local_ny);
165165

166166
/* prepare reading subarray */
167167
start[0] = 0;

examples/C/ghost_cell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pnetcdf_io(MPI_Comm comm, char *filename, int cmode, int len)
164164
/* allocate and initialize buffer with ghost cells on both ends of each dim */
165165
nghosts = 2;
166166
bufsize = (counts[0] + 2 * nghosts) * (counts[1] + 2 * nghosts);
167-
buf = (int *) malloc(bufsize * sizeof(int));
167+
buf = (int *) malloc(sizeof(int) * bufsize);
168168
for (i=0; i<counts[0]+2*nghosts; i++)
169169
for (j=0; j<counts[1]+2*nghosts; j++) {
170170
if (nghosts <= i && i < counts[0]+nghosts &&

examples/C/i_varn_int64.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ static MPI_Offset *** calloc_3D(size_t z, size_t y, size_t x)
130130
{
131131
if (z*y*x == 0) return NULL;
132132
int _j, _k;
133-
MPI_Offset ***buf = (MPI_Offset***) malloc(z * sizeof(MPI_Offset**));
134-
MPI_Offset **bufy = (MPI_Offset**) malloc(z*y * sizeof(MPI_Offset*));
133+
MPI_Offset ***buf = (MPI_Offset***) malloc(sizeof(MPI_Offset**) * z);
134+
MPI_Offset **bufy = (MPI_Offset**) malloc(sizeof(MPI_Offset*) * z*y);
135135
MPI_Offset *bufx = (MPI_Offset*) calloc(z*y*x, sizeof(MPI_Offset));
136136
for (_k=0; _k<z; _k++, bufy+=y) {
137137
buf[_k] = bufy;
@@ -168,7 +168,7 @@ static int check_contents(int ncid, int *varid)
168168
2, 2, 1, 0, 0, 0, 3, 3, 2, 2,
169169
3, 3, 3, 1, 2, 2, 2, 0, 0, 0}};
170170

171-
long long *r_buffer = (long long*) malloc(NY*NX * sizeof(long long));
171+
long long *r_buffer = (long long*) malloc(sizeof(long long) * NY*NX);
172172

173173
for (i=0; i<4; i++) {
174174
for (j=0; j<NY*NX; j++) r_buffer[j] = -1;
@@ -322,7 +322,7 @@ int main(int argc, char** argv)
322322
if (verbose) printf("req_lens[%d]=%d\n",i,req_lens[i]);
323323

324324
/* allocate I/O buffer and initialize its contents */
325-
buffer[i] = (long long*) malloc(req_lens[i] * sizeof(long long));
325+
buffer[i] = (long long*) malloc(sizeof(long long) * req_lens[i]);
326326
for (j=0; j<req_lens[i]; j++) buffer[i][j] = rank;
327327
}
328328

@@ -366,7 +366,7 @@ int main(int argc, char** argv)
366366
MPI_Datatype buftype;
367367
MPI_Type_vector(req_lens[i], 1, 2, MPI_LONG_LONG, &buftype);
368368
MPI_Type_commit(&buftype);
369-
buffer[i] = (long long*) malloc(req_lens[i] * 2 * sizeof(long long));
369+
buffer[i] = (long long*) malloc(sizeof(long long) * req_lens[i] * 2);
370370
for (j=0; j<req_lens[i]*2; j++) buffer[i][j] = rank;
371371

372372
err = ncmpi_iput_varn(ncid, varid[i], num_segs[i], starts[i],

examples/C/mput.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ int main(int argc, char** argv)
165165
else if (rank == 3) num_reqs = 4;
166166

167167
if (num_reqs > 0) {
168-
starts = (MPI_Offset**) malloc(num_reqs* sizeof(MPI_Offset*));
169-
counts = (MPI_Offset**) malloc(num_reqs* sizeof(MPI_Offset*));
168+
starts = (MPI_Offset**) malloc(sizeof(MPI_Offset*) * num_reqs);
169+
counts = (MPI_Offset**) malloc(sizeof(MPI_Offset*) * num_reqs);
170170
starts[0] = (MPI_Offset*) calloc(num_reqs*NDIMS, sizeof(MPI_Offset));
171171
counts[0] = (MPI_Offset*) calloc(num_reqs*NDIMS, sizeof(MPI_Offset));
172172
for (i=1; i<num_reqs; i++) {
@@ -226,9 +226,9 @@ int main(int argc, char** argv)
226226
*/
227227
}
228228

229-
nvarids = (int*) malloc(num_reqs * sizeof(int));
230-
bufcounts = (MPI_Offset*) malloc(num_reqs * sizeof(MPI_Offset));
231-
datatypes = (MPI_Datatype*) malloc(num_reqs * sizeof(MPI_Datatype));
229+
nvarids = (int*) malloc(sizeof(int) * num_reqs);
230+
bufcounts = (MPI_Offset*) malloc(sizeof(MPI_Offset) * num_reqs);
231+
datatypes = (MPI_Datatype*) malloc(sizeof(MPI_Datatype) * num_reqs);
232232
w_len = 0;
233233
for (i=0; i<num_reqs; i++) {
234234
nvarids[i] = varid;
@@ -238,11 +238,11 @@ int main(int argc, char** argv)
238238
}
239239

240240
/* allocate I/O buffer and initialize its contents */
241-
buffer = (int*) malloc(w_len * sizeof(int));
241+
buffer = (int*) malloc(sizeof(int) * w_len);
242242
for (i=0; i<w_len; i++) buffer[i] = rank;
243243

244244
/* set the buffer pointers to different offsets to the I/O buffer */
245-
bufs = (int**) malloc(num_reqs * sizeof(int*));
245+
bufs = (int**) malloc(sizeof(int*) * num_reqs);
246246
if (num_reqs > 0) bufs[0] = buffer;
247247
for (i=1; i<num_reqs; i++) bufs[i] = bufs[i-1] + bufcounts[i-1];
248248

0 commit comments

Comments
 (0)