From 7ae982f869b7939498808d1d921426f8c178f03f Mon Sep 17 00:00:00 2001 From: Chichi Lalescu <clalesc1@jhu.edu> Date: Sun, 22 Feb 2015 16:43:17 -0500 Subject: [PATCH] use std::copy instead of element-wise copy --- src/field_descriptor.cpp | 47 ++++++++++++++++------------------------ test.ipynb | 8 +++---- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/field_descriptor.cpp b/src/field_descriptor.cpp index 7c1cd5f7..f450f954 100644 --- a/src/field_descriptor.cpp +++ b/src/field_descriptor.cpp @@ -148,37 +148,28 @@ int field_descriptor::transpose( // for 3D transposition, the input data is messed up fftwf_plan tplan; ptrdiff_t dim1; - switch (this->ndims) + if (this->ndims == 3) { - case 2: - dim1 = this->sizes[1]; - break; - case 3: - // transpose the two local dimensions 1 and 2 - dim1 = this->sizes[1]*this->sizes[2]; - float *atmp; - atmp = (float*)malloc(dim1*sizeof(float)); - for (int k = 0; k < this->subsizes[0]; k++) - { - // put transposed slice in atmp - for (int j = 0; j < this->sizes[1]; j++) - for (int i = 0; i < this->sizes[2]; i++) - atmp[i*this->sizes[1] + j] = - input[(k*this->sizes[1] + j)*this->sizes[2] + i]; - // copy back transposed slice - for (int j = 0; j < this->sizes[2]; j++) - for (int i = 0; i < this->sizes[1]; i++) - input[(k*this->sizes[2] + j)*this->sizes[1] + i] = - atmp[j*this->sizes[1] + i]; - } - free(atmp); - break; - default: - return EXIT_FAILURE; - break; + // transpose the two local dimensions 1 and 2 + float *atmp; + atmp = fftwf_alloc_real(this->slice_size); + for (int k = 0; k < this->subsizes[0]; k++) + { + // put transposed slice in atmp + for (int j = 0; j < this->sizes[1]; j++) + for (int i = 0; i < this->sizes[2]; i++) + atmp[i*this->sizes[1] + j] = + input[(k*this->sizes[1] + j)*this->sizes[2] + i]; + // copy back transposed slice + std::copy( + atmp, + atmp + this->slice_size, + input + k*this->slice_size); + } + fftwf_free(atmp); } tplan = fftwf_mpi_plan_transpose( - this->sizes[0], dim1, + this->sizes[0], this->slice_size, input, output, this->comm, FFTW_ESTIMATE); diff --git a/test.ipynb b/test.ipynb index 23c546cd..6d762e04 100644 --- a/test.ipynb +++ b/test.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:e257a2ed63efdfaaad53572bf411ce350bc2da0748cbff2374ba511f3a837cbc" + "signature": "sha256:9cf3a6ad81b4021d61957c3d2805faad24a447da957672fc0268ff9b398c6dee" }, "nbformat": 3, "nbformat_minor": 0, @@ -187,7 +187,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 51 + "prompt_number": 54 }, { "cell_type": "code", @@ -215,11 +215,11 @@ "output_type": "stream", "stream": "stdout", "text": [ - "3.09944e-06\n" + "2.86102e-06\n" ] } ], - "prompt_number": 52 + "prompt_number": 55 }, { "cell_type": "code", -- GitLab