added Verlet scheme and NxN non-bonded functionality
[alexxy/gromacs.git] / src / mdlib / fft5d.c
index c606b6ad6724eeda32a3205f972666c1b05250d2..df8d310d2a6dc15f457b9742253da2b27e66e516 100644 (file)
@@ -60,9 +60,6 @@
 #ifdef GMX_OPENMP
 /* TODO: Do we still need this? Are we still planning ot use fftw + OpenMP? */
 #define FFT5D_THREADS
-#endif
-#ifdef FFT5D_THREADS
-#include "gmx_omp.h"
 /* requires fftw compiled with openmp */
 /* #define FFT5D_FFTW_THREADS (now set by cmake) */
 #endif
@@ -383,13 +380,31 @@ fft5d_plan fft5d_plan_3d(int NG, int MG, int KG, MPI_Comm comm[2], int flags, t_
     if (!(flags&FFT5D_NOMALLOC)) { 
         snew_aligned(lin, lsize, 32);
         snew_aligned(lout, lsize, 32);
-        snew_aligned(lout2, lsize, 32);
-        snew_aligned(lout3, lsize, 32);
+        if (nthreads > 1)
+        {
+            /* We need extra transpose buffers to avoid OpenMP barriers */
+            snew_aligned(lout2, lsize, 32);
+            snew_aligned(lout3, lsize, 32);
+        }
+        else
+        {
+            /* We can reuse the buffers to avoid cache misses */
+            lout2 = lin;
+            lout3 = lout;
+        }
     } else {
         lin = *rlin;
         lout = *rlout;
-        lout2 = *rlout2;
-        lout3 = *rlout3;
+        if (nthreads > 1)
+        {
+            lout2 = *rlout2;
+            lout3 = *rlout3;
+        }
+        else
+        {
+            lout2 = lin;
+            lout3 = lout;
+        }
     }
 
     plan = (fft5d_plan)calloc(1,sizeof(struct fft5d_plan_t));
@@ -509,6 +524,7 @@ fft5d_plan fft5d_plan_3d(int NG, int MG, int KG, MPI_Comm comm[2], int flags, t_
              */
 #pragma omp parallel for num_threads(nthreads) schedule(static) ordered
             for(t=0; t<nthreads; t++)
+#pragma omp ordered
             {
                 int tsize = ((t+1)*pM[s]*pK[s]/nthreads)-(t*pM[s]*pK[s]/nthreads);
 
@@ -904,7 +920,7 @@ void fft5d_execute(fft5d_plan plan,int thread,fft5d_time times) {
         }
 #endif
 
-        if (bParallelDim) {
+        if (bParallelDim || plan->nthreads == 1) {
             fftout = lout;
         }
         else
@@ -1091,6 +1107,14 @@ llToAll
 
 void fft5d_destroy(fft5d_plan plan) {
     int s,t;
+
+    /* Note that we expect plan->lin and plan->lout to be freed elsewhere */
+    if (plan->nthreads > 1)
+    {
+        free(plan->lout2);
+        free(plan->lout3);
+    }
+
     for (s=0;s<3;s++)
     {
         if (plan->p1d[s])