$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

coProcessor3D.hh

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #ifndef CO_PROCESSOR_3D_HH
00026 #define CO_PROCESSOR_3D_HH
00027 
00028 #include "coProcessors/coProcessor3D.h"
00029 #include "basicDynamics/isoThermalDynamics.h"
00030 #include <limits>
00031 
00032 namespace plb {
00033 
00034 template<typename T>
00035 int D3Q19ExampleCoProcessor3D<T>::addDomain(plint nx, plint ny, plint nz, T omega, int& domainHandle)
00036 {
00037     PLB_ASSERT( domains.size() < std::numeric_limits<int>::max() );
00038     domainHandle = (int)domains.size();
00039     Dynamics<T,descriptors::D3Q19Descriptor>* dynamics =
00040         new BGKdynamics<T,descriptors::D3Q19Descriptor>(omega);
00041     domains.insert (
00042             std::pair<int, BlockLattice3D<T,descriptors::D3Q19Descriptor> > (
00043                 domainHandle, BlockLattice3D<T,descriptors::D3Q19Descriptor>(nx, ny, nz, dynamics) ) );
00044     return 1; // Success.
00045 }
00046 
00047 template<typename T>
00048 int D3Q19ExampleCoProcessor3D<T>::send(int domainHandle, Box3D const& subDomain, std::vector<char> const& data)
00049 {
00050     typename std::map<int, BlockLattice3D<T,descriptors::D3Q19Descriptor> >::iterator it
00051         = domains.find(domainHandle);
00052     PLB_ASSERT( it != domains.end() );
00053     BlockLattice3D<T,descriptors::D3Q19Descriptor>& lattice = it->second;
00054     lattice.getDataTransfer().receive(subDomain, data, modif::staticVariables);
00055     return 1; // Success.
00056 }
00057 
00058 template<typename T>
00059 int D3Q19ExampleCoProcessor3D<T>::receive(int domainHandle, Box3D const& subDomain, std::vector<char>& data) const
00060 {
00061     typename std::map<int, BlockLattice3D<T,descriptors::D3Q19Descriptor> >::const_iterator it
00062         = domains.find(domainHandle);
00063     PLB_ASSERT( it != domains.end() );
00064     BlockLattice3D<T,descriptors::D3Q19Descriptor> const& lattice = it->second;
00065     lattice.getDataTransfer().send(subDomain, data, modif::staticVariables);
00066     return 1; // Success.
00067 }
00068 
00069 template<typename T>
00070 int D3Q19ExampleCoProcessor3D<T>::collideAndStream(int domainHandle)
00071 {
00072     typename std::map<int, BlockLattice3D<T,descriptors::D3Q19Descriptor> >::iterator it
00073         = domains.find(domainHandle);
00074     PLB_ASSERT( it != domains.end() );
00075     BlockLattice3D<T,descriptors::D3Q19Descriptor>& lattice = it->second;
00076     lattice.collideAndStream(lattice.getBoundingBox());
00077     return 1; // Success.
00078 }
00079 
00080 template<typename T>
00081 D3Q19CudaCoProcessor3D<T>::D3Q19CudaCoProcessor3D()
00082     {
00083         /*
00084         PyObject *pName, *pModule, *pFunc, *driver, *value;
00085 
00086         char **sailfishArgs;
00087         sailfishArgs = new char*[1];
00088         sailfishArgs[0] = "cavity3D.py";
00089         Py_Initialize();
00090         PySys_SetArgvEx(1, sailfishArgs, 1);
00091         delete [] sailfishArgs;
00092 
00093         pName = PyString_FromString("cavity3D");
00094         pModule = PyImport_Import(pName);
00095         Py_CLEAR(pName);
00096         this->geometryClass = PyObject_GetAttrString(pModule, "Geometry");
00097         this->simulationClass = PyObject_GetAttrString(pModule, "Simulation");
00098         Py_CLEAR(pModule);
00099 
00100         pName = PyString_FromString("pycuda");
00101         this->pyCUDA = PyImport_Import(pName);
00102         driver = PyObject_GetAttrString(pyCUDA, "driver");
00103         this->copyFunctionHtoD = PyObject_GetAttrString(driver, "memcpy_htod");
00104         this->copyFunctionDtoH = PyObject_GetAttrString(driver, "memcpy_dtoh");
00105         Py_CLEAR(pName);
00106         */
00107     }
00108 
00109 template<typename T>
00110 D3Q19CudaCoProcessor3D<T>::~D3Q19CudaCoProcessor3D()
00111     {
00112         /*
00113         Py_CLEAR(this->buffer);
00114         delete [] this->data;
00115 
00116         Py_CLEAR(this->copyFunctionDtoH);
00117         Py_CLEAR(this->copyFunctionHtoD);
00118         Py_CLEAR(this->simulation);
00119         Py_CLEAR(this->pyCUDA);
00120 
00121         Py_CLEAR(this->simulationClass);
00122         Py_CLEAR(this->geometryClass);
00123         Py_Finalize();
00124         */
00125     }
00126 
00127 template<typename T>
00128 int D3Q19CudaCoProcessor3D<T>::addDomain(plint nx, plint ny, plint nz, T omega, int& domainHandle)
00129 //int D3Q19CudaCoProcessor3D<T>::addDomain(Box3D const& domain, T omega)
00130     {
00131         /*
00132 
00133         this->nx = nx;
00134         this->ny = ny;
00135         this->nz = nz;
00136         this->basis = 19;
00137         this->simulation = createCUDASimulation();
00138         prepareSimulationToRun();
00139 
00140         PyObject *name, *grid, *s, *strides;
00141         name = PyString_FromString("grid");
00142         grid = PyObject_GetAttr(this->simulation, name);
00143         Py_CLEAR(name);
00144 
00145         name = PyString_FromString("get_dist_bytes");
00146         s = PyObject_CallMethodObjArgs(this->simulation, name, grid, NULL);
00147         Py_CLEAR(name);
00148         Py_CLEAR(grid);
00149         size = PyInt_AsLong(s);
00150         Py_CLEAR(s);
00151 
00152         name = PyString_FromString("float");
00153         grid = PyObject_GetAttr(this->simulation, name);
00154         Py_CLEAR(name);
00155         name = PyString_FromString("_get_strides");
00156         s = PyObject_CallMethodObjArgs(this->simulation, name, grid, NULL);
00157         Py_CLEAR(name);
00158         Py_CLEAR(grid);
00159         strides = PyTuple_GetItem(s, 0);
00160         this->floatSize = PyInt_AsLong(PyTuple_GetItem(strides, 2));
00161         size /= floatSize;
00162         this->strideZSailfish = PyInt_AsLong(PyTuple_GetItem(s, 1));
00163         this->strideYSailfish = PyInt_AsLong(PyTuple_GetItem(strides, 0))/floatSize;
00164         this->strideXSailfish = PyInt_AsLong(PyTuple_GetItem(strides, 1))/floatSize;
00165         Py_CLEAR(s);
00166 
00167         this->data = new float[size];
00168         this->buffer = PyBuffer_FromReadWriteMemory(data, size*floatSize);
00169 
00170         this->strideYPalabos = this->basis*this->nz*this->ny;
00171         this->strideZPalabos = this->basis*this->nz;
00172         */
00173 
00174         return 1;
00175     }
00176 
00177 #define SAILFISHD3Q19(x, y, z, b) (b*strideZSailfish+z*strideYSailfish+y*strideXSailfish+x)
00178 #define PALABOSD3Q19(x, y, z, b) (x*strideYPalabos+y*strideZPalabos+z*basis+b)
00179 
00180 template<typename T>
00181 int D3Q19CudaCoProcessor3D<T>::send(int domainHandle, Box3D const& subDomain, std::vector<char> const& data)
00182     {
00183         /*
00184         PyObject *memories, *memory, *args;
00185         const T *palabosData = reinterpret_cast<const T *>(&data[0]);
00186 
00187         memories = PyObject_CallMethod(this->simulation, "curr_dists", NULL);
00188         memory = PyList_GetItem(memories, 0);
00189         Py_INCREF(memory);
00190         Py_CLEAR(memories);
00191 
00192         int x0, x1, y0, y1, z0, z1;
00193         x0 = subDomain.x0;
00194         x1 = subDomain.x1;
00195         y0 = subDomain.y0;
00196         y1 = subDomain.y1;
00197         z0 = subDomain.z0;
00198         z1 = subDomain.z1;
00199 // Palabos population: src/latticeBoltzman/nearestNeighborLattices3D.hh:159
00200 // Sailfish population: sailfish/sym.py:299
00201 #if 0
00202         std::cout << "s " << palabosData[PALABOSD3Q19(1, 1, 1, 0)] << " ";
00203         std::cout << palabosData[PALABOSD3Q19(1, 1, 1, 10)] << " ";
00204         std::cout << palabosData[PALABOSD3Q19(10, 10, 10, 0)] << " ";
00205         std::cout << palabosData[PALABOSD3Q19(10, 10, 10, 17)] << std::endl;
00206         for (int i = 0; i < size; i++) {
00207                 this->data[i] = 0.0f;
00208         }
00209 #endif
00210         for (int i = z0; i <= z1; i++) {
00211             for (int j = y0; j <= y1; j++) {
00212                 for (int k = x0; k <= x1; k++) {
00213                     this->data[SAILFISHD3Q19(k, j, i, 0)] =
00214                         palabosData[PALABOSD3Q19(k, j, i, 0)] +descriptors::D3Q19Descriptor<T>::t[0];
00215                     this->data[SAILFISHD3Q19(k, j, i, 1)] =
00216                         palabosData[PALABOSD3Q19(k, j, i, 10)]+descriptors::D3Q19Descriptor<T>::t[10];
00217                     this->data[SAILFISHD3Q19(k, j, i, 2)] =
00218                         palabosData[PALABOSD3Q19(k, j, i, 1)] +descriptors::D3Q19Descriptor<T>::t[1];
00219                     this->data[SAILFISHD3Q19(k, j, i, 3)] =
00220                         palabosData[PALABOSD3Q19(k, j, i, 11)]+descriptors::D3Q19Descriptor<T>::t[11];
00221                     this->data[SAILFISHD3Q19(k, j, i, 4)] =
00222                         palabosData[PALABOSD3Q19(k, j, i, 2)] +descriptors::D3Q19Descriptor<T>::t[2];
00223                     this->data[SAILFISHD3Q19(k, j, i, 5)] =
00224                         palabosData[PALABOSD3Q19(k, j, i, 12)]+descriptors::D3Q19Descriptor<T>::t[12];
00225                     this->data[SAILFISHD3Q19(k, j, i, 6)] =
00226                         palabosData[PALABOSD3Q19(k, j, i, 3)] +descriptors::D3Q19Descriptor<T>::t[3];
00227                     this->data[SAILFISHD3Q19(k, j, i, 7)] =
00228                         palabosData[PALABOSD3Q19(k, j, i, 13)]+descriptors::D3Q19Descriptor<T>::t[13];
00229                     this->data[SAILFISHD3Q19(k, j, i, 8)] =
00230                         palabosData[PALABOSD3Q19(k, j, i, 5)] +descriptors::D3Q19Descriptor<T>::t[5];
00231                     this->data[SAILFISHD3Q19(k, j, i, 9)] =
00232                         palabosData[PALABOSD3Q19(k, j, i, 14)]+descriptors::D3Q19Descriptor<T>::t[14];
00233                     this->data[SAILFISHD3Q19(k, j, i, 10)] =
00234                         palabosData[PALABOSD3Q19(k, j, i, 4)] +descriptors::D3Q19Descriptor<T>::t[4];
00235                     this->data[SAILFISHD3Q19(k, j, i, 11)] =
00236                         palabosData[PALABOSD3Q19(k, j, i, 17)]+descriptors::D3Q19Descriptor<T>::t[17];
00237                     this->data[SAILFISHD3Q19(k, j, i, 12)] =
00238                         palabosData[PALABOSD3Q19(k, j, i, 9)] +descriptors::D3Q19Descriptor<T>::t[9];
00239                     this->data[SAILFISHD3Q19(k, j, i, 13)] =
00240                         palabosData[PALABOSD3Q19(k, j, i, 18)]+descriptors::D3Q19Descriptor<T>::t[18];
00241                     this->data[SAILFISHD3Q19(k, j, i, 14)] =
00242                         palabosData[PALABOSD3Q19(k, j, i, 8)] +descriptors::D3Q19Descriptor<T>::t[8];
00243                     this->data[SAILFISHD3Q19(k, j, i, 15)] =
00244                         palabosData[PALABOSD3Q19(k, j, i, 15)]+descriptors::D3Q19Descriptor<T>::t[15];
00245                     this->data[SAILFISHD3Q19(k, j, i, 16)] =
00246                         palabosData[PALABOSD3Q19(k, j, i, 7)] +descriptors::D3Q19Descriptor<T>::t[7];
00247                     this->data[SAILFISHD3Q19(k, j, i, 17)] =
00248                         palabosData[PALABOSD3Q19(k, j, i, 16)]+descriptors::D3Q19Descriptor<T>::t[16];
00249                     this->data[SAILFISHD3Q19(k, j, i, 18)] =
00250                         palabosData[PALABOSD3Q19(k, j, i, 6)] +descriptors::D3Q19Descriptor<T>::t[6];
00251                 }
00252             }
00253         }
00254 #if 0
00255         std::cout << "s " << this->data[SAILFISHD3Q19(10, 10, 10, 0)] << std::endl;
00256 #endif
00257         args = PyTuple_New(2);
00258         PyTuple_SetItem(args, 0, memory);
00259         Py_INCREF(this->buffer);
00260         PyTuple_SetItem(args, 1, this->buffer);
00261         PyObject_Call(this->copyFunctionHtoD, args, NULL);
00262         Py_CLEAR(args);
00263 #if 0
00264         std::cout << "s " << this->data[SAILFISHD3Q19(10, 10, 10, 0)] << std::endl;
00265 #endif
00266         */
00267         return 1;
00268     }
00269 template<typename T>
00270 int D3Q19CudaCoProcessor3D<T>::receive(int domainHandle, Box3D const& subDomain, std::vector<char>& data) const
00271     {
00272         /*
00273         PyObject *memories, *memory, *args;
00274 
00275 #if 0
00276         for (int i = 0; i < this->size; i++) {
00277                 this->data[i] = 0.0f;
00278         }
00279 #endif
00280 
00281         memories = PyObject_CallMethod(this->simulation, "curr_dists", NULL);
00282         memory = PyList_GetItem(memories, 0);
00283         Py_INCREF(memory);
00284         Py_CLEAR(memories);
00285 
00286         args = PyTuple_New(2);
00287         Py_INCREF(this->buffer);
00288         PyTuple_SetItem(args, 0, this->buffer);
00289         PyTuple_SetItem(args, 1, memory);
00290         PyObject_Call(this->copyFunctionDtoH, args, NULL);
00291         Py_CLEAR(args);
00292 
00293         int x0, x1, y0, y1, z0, z1, size;
00294         x0 = subDomain.x0;
00295         x1 = subDomain.x1;
00296         y0 = subDomain.y0;
00297         y1 = subDomain.y1;
00298         z0 = subDomain.z0;
00299         z1 = subDomain.z1;
00300         size = (x1-x0+1)*(y1-y0+1)*(z1-z0+1)*this->basis*sizeof(T);
00301         if (data.size() != size) {
00302             data.resize(size);
00303         }
00304         T *palabosData = reinterpret_cast<T *>(&data[0]);
00305 #if 0
00306         for (int i = 0; i < size/sizeof(T); i++) {
00307                 palabosData[i] = 0.0;
00308         }
00309 
00310         std::cout << "r " << this->data[SAILFISHD3Q19(1, 1, 1, 0)] << std::endl;
00311 #endif
00312         for (int i = z0; i <= z1; i++) {
00313             for (int j = y0; j <= y1; j++) {
00314                 for (int k = x0; k <= x1; k++) {
00315                     palabosData[PALABOSD3Q19(k, j, i, 0)] = 
00316                         this->data[SAILFISHD3Q19(k, j, i, 0)] -descriptors::D3Q19Descriptor<T>::t[0];
00317                     palabosData[PALABOSD3Q19(k, j, i, 10)] = 
00318                         this->data[SAILFISHD3Q19(k, j, i, 1)] -descriptors::D3Q19Descriptor<T>::t[10];
00319                     palabosData[PALABOSD3Q19(k, j, i, 1)] = 
00320                         this->data[SAILFISHD3Q19(k, j, i, 2)] -descriptors::D3Q19Descriptor<T>::t[1];
00321                     palabosData[PALABOSD3Q19(k, j, i, 11)] = 
00322                         this->data[SAILFISHD3Q19(k, j, i, 3)] -descriptors::D3Q19Descriptor<T>::t[11];
00323                     palabosData[PALABOSD3Q19(k, j, i, 2)] = 
00324                         this->data[SAILFISHD3Q19(k, j, i, 4)] -descriptors::D3Q19Descriptor<T>::t[2];
00325                     palabosData[PALABOSD3Q19(k, j, i, 12)] = 
00326                         this->data[SAILFISHD3Q19(k, j, i, 5)] -descriptors::D3Q19Descriptor<T>::t[12];
00327                     palabosData[PALABOSD3Q19(k, j, i, 3)] = 
00328                         this->data[SAILFISHD3Q19(k, j, i, 6)] -descriptors::D3Q19Descriptor<T>::t[3];
00329                     palabosData[PALABOSD3Q19(k, j, i, 13)] = 
00330                         this->data[SAILFISHD3Q19(k, j, i, 7)] -descriptors::D3Q19Descriptor<T>::t[13];
00331                     palabosData[PALABOSD3Q19(k, j, i, 5)] = 
00332                         this->data[SAILFISHD3Q19(k, j, i, 8)] -descriptors::D3Q19Descriptor<T>::t[5];
00333                     palabosData[PALABOSD3Q19(k, j, i, 14)] = 
00334                         this->data[SAILFISHD3Q19(k, j, i, 8)] -descriptors::D3Q19Descriptor<T>::t[14];
00335                     palabosData[PALABOSD3Q19(k, j, i, 4)] = 
00336                         this->data[SAILFISHD3Q19(k, j, i, 10)]-descriptors::D3Q19Descriptor<T>::t[4];
00337                     palabosData[PALABOSD3Q19(k, j, i, 17)] = 
00338                         this->data[SAILFISHD3Q19(k, j, i, 11)]-descriptors::D3Q19Descriptor<T>::t[17];
00339                     palabosData[PALABOSD3Q19(k, j, i, 9)] = 
00340                         this->data[SAILFISHD3Q19(k, j, i, 12)]-descriptors::D3Q19Descriptor<T>::t[9];
00341                     palabosData[PALABOSD3Q19(k, j, i, 18)] = 
00342                         this->data[SAILFISHD3Q19(k, j, i, 13)]-descriptors::D3Q19Descriptor<T>::t[18];
00343                     palabosData[PALABOSD3Q19(k, j, i, 8)] = 
00344                         this->data[SAILFISHD3Q19(k, j, i, 14)]-descriptors::D3Q19Descriptor<T>::t[8];
00345                     palabosData[PALABOSD3Q19(k, j, i, 15)] = 
00346                         this->data[SAILFISHD3Q19(k, j, i, 15)]-descriptors::D3Q19Descriptor<T>::t[15];
00347                     palabosData[PALABOSD3Q19(k, j, i, 7)] = 
00348                         this->data[SAILFISHD3Q19(k, j, i, 16)]-descriptors::D3Q19Descriptor<T>::t[7];
00349                     palabosData[PALABOSD3Q19(k, j, i, 16)] = 
00350                         this->data[SAILFISHD3Q19(k, j, i, 17)]-descriptors::D3Q19Descriptor<T>::t[16];
00351                     palabosData[PALABOSD3Q19(k, j, i, 6)] = 
00352                         this->data[SAILFISHD3Q19(k, j, i, 18)]-descriptors::D3Q19Descriptor<T>::t[6];
00353                 }
00354             }
00355         }
00356         */
00357         return 1;
00358     }
00359 
00360 template<typename T>
00361 int D3Q19CudaCoProcessor3D<T>::collideAndStream(int domainHandle)
00362     {
00363         /*
00364         PyObject_CallMethod(this->simulation, "sim_step", NULL);
00365         */
00366         return 1;
00367     }
00368 
00369         /*
00370 template<typename T>
00371 PyObject * D3Q19CudaCoProcessor3D<T>::createCUDASimulation(void)
00372     {
00373         PyObject *result = NULL;
00374         PyObject *pSailfishArgsCUDA, *args, *kw, *value;
00375 
00376         pSailfishArgsCUDA = PyDict_New();
00377         value = PyString_FromString("cuda");
00378         PyDict_SetItemString(pSailfishArgsCUDA, "backend", value);
00379         Py_CLEAR(value);
00380         value = PyString_FromString("D3Q19");
00381         PyDict_SetItemString(pSailfishArgsCUDA, "grid", value);
00382         Py_CLEAR(value);
00383         value = PyInt_FromLong(this->nx);
00384         PyDict_SetItemString(pSailfishArgsCUDA, "lat_nx", value);
00385         Py_CLEAR(value);
00386         value = PyInt_FromLong(this->ny);
00387         PyDict_SetItemString(pSailfishArgsCUDA, "lat_ny", value);
00388         Py_CLEAR(value);
00389         value = PyInt_FromLong(this->nz);
00390         PyDict_SetItemString(pSailfishArgsCUDA, "lat_nz", value);
00391         Py_CLEAR(value);
00392         PyDict_SetItemString(pSailfishArgsCUDA, "verbose", Py_True);
00393         PyDict_SetItemString(pSailfishArgsCUDA, "batch", Py_True);
00394         value = PyInt_FromLong(64);
00395         PyDict_SetItemString(pSailfishArgsCUDA, "max_iters", value);
00396         Py_CLEAR(value);
00397 
00398         args = PyTuple_New(1);
00399         Py_INCREF(this->geometryClass);
00400         PyTuple_SetItem(args, 0, this->geometryClass);
00401 
00402         kw = PyDict_New();
00403         PyDict_SetItemString(kw, "defaults", pSailfishArgsCUDA);
00404         Py_CLEAR(pSailfishArgsCUDA);
00405         
00406         result = PyObject_Call(this->simulationClass, args, kw);
00407 
00408         Py_CLEAR(kw);
00409         Py_CLEAR(args);
00410 
00411         return result;
00412     }
00413         */
00414         /*
00415 template<typename T>
00416 void D3Q19CudaCoProcessor3D<T>:: prepareSimulationToRun(void)
00417     {
00418         PyObject_CallMethod(this->simulation, "_init_shape", NULL);
00419         PyObject_CallMethod(this->simulation, "_init_vis", NULL);
00420         PyObject_CallMethod(this->simulation, "_init_geo", NULL);
00421         PyObject_CallMethod(this->simulation, "_init_post_geo", NULL);
00422         PyObject_CallMethod(this->simulation, "_init_code", NULL);
00423         PyObject_CallMethod(this->simulation, "_init_compute_fields", NULL);
00424         PyObject_CallMethod(this->simulation, "_init_compute_kernels", NULL);
00425         PyObject_CallMethod(this->simulation, "_init_compute_ic", NULL);
00426         PyObject_CallMethod(this->simulation, "_init_output", NULL);
00427     }
00428         */
00429 
00430 }  // namespace plb
00431 
00432 #endif  // CO_PROCESSOR_3D_HH
00433