From c5ec34318b0f5560f8ac5929961d16c5a65542b0 Mon Sep 17 00:00:00 2001 From: Jose Luis Cercos Pita Date: Mon, 30 Jul 2012 14:30:13 +0200 Subject: [PATCH] Implemented initialization stage --- src/Mod/Ship/CMakeLists.txt | 4 + src/Mod/Ship/Makefile.am | 7 +- src/Mod/Ship/OpenCL/simInit.cl | 99 ++++++++++ src/Mod/Ship/shipUtils/Paths.pyc | Bin 0 -> 1379 bytes src/Mod/Ship/shipUtils/__init__.pyc | Bin 0 -> 159 bytes src/Mod/Ship/simRun/Sim/Utils.py | 57 ++++++ src/Mod/Ship/simRun/Sim/Utils.py~ | 58 ++++++ src/Mod/Ship/simRun/Sim/__init__.py | 24 +++ src/Mod/Ship/simRun/Sim/__init__.py~ | 57 ++++++ src/Mod/Ship/simRun/Sim/initialization.py | 113 ++++++++++++ src/Mod/Ship/simRun/Sim/initialization.py~ | 117 ++++++++++++ src/Mod/Ship/simRun/Simulation.py | 202 +++++++++++---------- src/Mod/Ship/simRun/Simulation.py~ | 107 +++++++++++ 13 files changed, 747 insertions(+), 98 deletions(-) create mode 100644 src/Mod/Ship/OpenCL/simInit.cl create mode 100644 src/Mod/Ship/shipUtils/Paths.pyc create mode 100644 src/Mod/Ship/shipUtils/__init__.pyc create mode 100644 src/Mod/Ship/simRun/Sim/Utils.py create mode 100644 src/Mod/Ship/simRun/Sim/Utils.py~ create mode 100644 src/Mod/Ship/simRun/Sim/__init__.py create mode 100644 src/Mod/Ship/simRun/Sim/__init__.py~ create mode 100644 src/Mod/Ship/simRun/Sim/initialization.py create mode 100644 src/Mod/Ship/simRun/Sim/initialization.py~ create mode 100644 src/Mod/Ship/simRun/Simulation.py~ diff --git a/src/Mod/Ship/CMakeLists.txt b/src/Mod/Ship/CMakeLists.txt index ef3d635a5..5f0314abd 100644 --- a/src/Mod/Ship/CMakeLists.txt +++ b/src/Mod/Ship/CMakeLists.txt @@ -56,6 +56,7 @@ SET(ShipExamples_SRCS Examples/s60_katamaran.fcstd Examples/wigley.fcstd Examples/wigley_katamaran.fcstd + OpenCL/simInit.cl ) SOURCE_GROUP("shipexamples" FILES ${ShipExamples_SRCS}) @@ -144,6 +145,9 @@ SET(SimRun_SRCS simRun/Simulation.py simRun/TaskPanel.py simRun/TaskPanel.ui + simRun/Sim/__init__.py + simRun/Sim/initialization.py + simRun/Sim/Utils.py ) SOURCE_GROUP("simrun" FILES ${SimRun_SRCS}) diff --git a/src/Mod/Ship/Makefile.am b/src/Mod/Ship/Makefile.am index 6aa83570c..084e9a877 100644 --- a/src/Mod/Ship/Makefile.am +++ b/src/Mod/Ship/Makefile.am @@ -53,6 +53,7 @@ nobase_data_DATA = \ Examples/s60_katamaran.fcstd \ Examples/wigley.fcstd \ Examples/wigley_katamaran.fcstd \ + OpenCL/simInit.cl \ shipLoadExample/__init__.py \ shipLoadExample/TaskPanel.py \ shipLoadExample/TaskPanel.ui \ @@ -96,7 +97,11 @@ nobase_data_DATA = \ simRun/__init__.py \ simRun/Simulation.py \ simRun/TaskPanel.py \ - simRun/TaskPanel.ui + simRun/TaskPanel.ui \ + simRun/Sim/__init__.py \ + simRun/Sim/initialization.py + simRun/Sim/Utils.py + CLEANFILES = $(BUILT_SOURCES) diff --git a/src/Mod/Ship/OpenCL/simInit.cl b/src/Mod/Ship/OpenCL/simInit.cl new file mode 100644 index 000000000..7bb8a3c2f --- /dev/null +++ b/src/Mod/Ship/OpenCL/simInit.cl @@ -0,0 +1,99 @@ +/* + * ----------------------------------------------------------------------- + * + * This source file is part of AQUA-gpusph. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA + * + * + * Authors: + * - Cercos Pita, Jose Luis + * - Miguel Gonzalez, Leo + * - Saelices, Jaime + * - Souto Iglesias, Antonio + * + * ----------------------------------------------------------------------- + */ + +#ifndef M_PI + #define M_PI 3,14159265359 +#endif + +#ifdef _g + #error '_g' is already defined. +#endif +#define _g __global + +#ifdef _l + #error '_l' is already defined. +#endif +#define _l __local + +#ifdef _c + #error '_c' is already defined. +#endif +#define _c __constant + +#ifndef _grav + #define _grav 9.81 +#endif + +/** Setup velocity and acceleration potential for initial time step. + * @param pos Cell position. + * @param v Cell velocity. + * @param f Cell acceleration. + * @param waves Waves (A,T,phase,heading) + * @param phi Velocity potential. + * @param Phi Acceleration potential + * @param N Number of cell elements at each direction. + * @param n Number of waves. + */ +__kernel void FS(_g float4* pos, _g float4* v, _g float4* f, + _g float4* waves, _g float* phi, _g float* Phi, + uint2 N, uint n) +{ + // find position in global arrays + unsigned int i = get_global_id(0); + unsigned int j = get_global_id(1); + if( (i >= N.x) || (j >= N.y) ) + return; + unsigned int id = i*N.y + j; + + // ---- | ------------------------ | ---- + // ---- V ---- Your code here ---- V ---- + + unsigned int w; + for(w=0;wDcHh@xzwJ zvtJ+3pH_PR5LF#i)k!%qXYh0D*ux%k95J3dW8u(Ed~z!N`3G=0vc_}{y1ogyQx=2# z^4NIPO;kD`;loLN1A@8Aop)_Ed4`7#@0Qa0HdG{wJA@PuQAk{w3X3ggCiSKJHbAm8 z$zuM#Wx?4rUKANSRC@07v5TdF-D>7rh*USvsu$QfR#CXEw$+x30-272W;vZrY%rZ$ zApy^Y;g+i12~7V8b)J@(2e)_!@Q+KiV@P7zw>f{00jwnk=U&j@6jB(2v4f4{0a3W2 z(gg&ZTS(f6fX)FI^BEMqQ8d$Z^_;aMfDR$;vB^Y~r_gCx%7HMRJ@260 zy%TU(Y#1q;=QdFxkk5I)B4b`;=1S=DIT=sQp|z#0t?TgP44hGp=Jzt2M@VyNJWu8b z030FX8>rkC7FuhlNHtW5-%GUtxy|h9$yH)L&oANeH1F|5g-bm)^q9@7dPVw=*Nd^) StMAu`XOM-|J3+J6Z2SeBBtl;R literal 0 HcmV?d00001 diff --git a/src/Mod/Ship/shipUtils/__init__.pyc b/src/Mod/Ship/shipUtils/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..711c3c8e986a40fc9e96644870d2ee3a44185970 GIT binary patch literal 159 zcmZSn%**v&T_7Nt0SXv_v;zR5ttd4$IWa{aMC*bW#YM^bzWFKo!5Nta`o%yZv?Md9SU)~KGcU6wK3=b& XvV;R@m`!eeN@-529mvXJAZ7pnse&aU literal 0 HcmV?d00001 diff --git a/src/Mod/Ship/simRun/Sim/Utils.py b/src/Mod/Ship/simRun/Sim/Utils.py new file mode 100644 index 000000000..1d085b9ec --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/Utils.py @@ -0,0 +1,57 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# FreeCAD +from shipUtils import Paths + +# pyOpenCL +import pyopencl as cl +import numpy as np + +# Standard +import math + +def loadProgram(context, file): + """ Loads a file and comnpile it. + @param context OpenCL context where apply. + @param file File to load and compile. + @return Ready to use OpenCL program. + """ + f = open(file, 'r') + str = "".join(f.readlines()) + return cl.Program(context, str).build() + +def clPath(): + """ Gets the OpenCL kernels path + @return OpenCL kernels path + """ + path = Paths.modulePath() + "/OpenCL" + return path + +def globalSize(n): + """ Compute global size from amount of data. + @param n Amount of data. + @return global size. + """ + localSize = 256.0 + return int(math.ceil(n/localSize)*localSize) diff --git a/src/Mod/Ship/simRun/Sim/Utils.py~ b/src/Mod/Ship/simRun/Sim/Utils.py~ new file mode 100644 index 000000000..7c37bfab2 --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/Utils.py~ @@ -0,0 +1,58 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# FreeCAD +from shipUtils import Paths + +# pyOpenCL +import pyopencl as cl +import numpy as np + +# Standard +import math + +def loadProgram(context, file): + """ Loads a file and comnpile it. + @param context OpenCL context where apply. + @param file File to load and compile. + @return Ready to use OpenCL program. + """ + f = open(file, 'r') + str = "".join(f.readlines()) + print(str) + return cl.Program(context, str).build() + +def clPath(): + """ Gets the OpenCL kernels path + @return OpenCL kernels path + """ + path = Paths.modulePath() + "/OpenCL" + return path + +def globalSize(n): + """ Compute global size from amount of data. + @param n Amount of data. + @return global size. + """ + localSize = 256.0 + return int(math.ceil(n/localSize)*localSize) diff --git a/src/Mod/Ship/simRun/Sim/__init__.py b/src/Mod/Ship/simRun/Sim/__init__.py new file mode 100644 index 000000000..2fcb8e495 --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/__init__.py @@ -0,0 +1,24 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import initialization, Utils \ No newline at end of file diff --git a/src/Mod/Ship/simRun/Sim/__init__.py~ b/src/Mod/Ship/simRun/Sim/__init__.py~ new file mode 100644 index 000000000..30db4bcdb --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/__init__.py~ @@ -0,0 +1,57 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# FreeCAD +from shipUtils import Paths + +# pyOpenCL +import pyopencl as cl +import numpy as np + +# Standard +import math + +def loadProgram(context, file): + """ Loads a file and comnpile it. + @param context OpenCL context where apply. + @param file File to load and compile. + @return Ready to use OpenCL program. + """ + f = open(file, 'r') + str = "".join(f.readlines()) + return cl.Program(context, str).build() + +def clPath(): + """ Gets the OpenCL kernels path + @return OpenCL kernels path + """ + path = Paths.modulePath() + "/OpenCL" + return path + +def globalSize(n): + """ Compute global size from amount of data. + @param n Amount of data. + @return global size. + """ + localSize = 256 + return int(math.ceil(n/localSize)) diff --git a/src/Mod/Ship/simRun/Sim/initialization.py b/src/Mod/Ship/simRun/Sim/initialization.py new file mode 100644 index 000000000..90bdf5bfa --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/initialization.py @@ -0,0 +1,113 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# Simulation stuff +from Utils import * + +# pyOpenCL +import pyopencl as cl +import numpy as np + +class perform: + def __init__(self, context, queue, FSmesh, waves): + """ Constructor, includes program loading. + @param context OpenCL context where apply. + @param queue OpenCL command queue. + @param FSmesh Initial free surface mesh. + @param waves Considered simulation waves (A,T,phi,heading). + """ + self.context = context + self.queue = queue + self.program = loadProgram(context, clPath() + "/simInit.cl") + self.loadData(FSmesh, waves) + self.execute() + + def loadData(self, FSmesh, waves): + """ Convert data to numpy format, and create OpenCL + buffers. + @param FSmesh Initial free surface mesh. + @param waves Considered simulation waves (A,T,phi,heading). + """ + mf = cl.mem_flags + nx = len(FSmesh) + ny = len(FSmesh[0]) + nW = len(waves) + # Mesh data + p = np.ndarray((nx*ny, 4), dtype=np.float32) + n = np.ndarray((nx*ny, 4), dtype=np.float32) + a = np.ndarray((nx*ny, 1), dtype=np.float32) + for i in range(0, nx): + for j in range(0, ny): + id = i*ny + j + pos = FSmesh[i][j].pos + normal = FSmesh[i][j].normal + area = FSmesh[i][j].area + p[id,0] = pos.x + p[id,1] = pos.y + p[id,2] = pos.z + p[id,3] = 1. + n[id,0] = normal.x + n[id,1] = normal.y + n[id,2] = normal.z + n[id,3] = 0. + a[id,0] = area + p_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=p) + n_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=n) + a_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=a) + v_cl = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny*4 * np.dtype('float32').itemsize) + f_cl = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny*4 * np.dtype('float32').itemsize) + phi = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny * np.dtype('float32').itemsize) + Phi = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny * np.dtype('float32').itemsize) + self.fs = {'Nx':nx, 'Ny':ny, 'pos':p_cl, 'vel':v_cl, 'acc':f_cl, \ + 'normal':n_cl, 'area':a_cl, 'velPot':phi, 'accPot':Phi} + # Waves data + w = np.ndarray((nW, 4), dtype=np.float32) + for i in range(0,nW): + w[i,0] = waves[i][0] + w[i,1] = waves[i][1] + w[i,2] = waves[i][2] + w[i,3] = waves[i][3] + w_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=w) + self.waves = {'N':nW, 'data':w_cl} + # Ensure that all data has been written + self.queue.finish() + + def execute(self): + """ Compute initial conditions. """ + # Global size computation + N = np.ndarray((2, 1), dtype=np.uint32) + N[0] = self.fs['Nx'] + N[1] = self.fs['Ny'] + n = np.uint32(self.waves['N']) + gSize = (globalSize(N[0]),globalSize(N[1]),) + # Kernel arguments + kernelargs = (self.fs['pos'], + self.fs['vel'], + self.fs['acc'], + self.waves['data'], + self.fs['velPot'], + self.fs['accPot'], + N, n) + # Kernel launch + self.program.FS(self.queue, gSize, None, *(kernelargs)) + self.queue.finish() diff --git a/src/Mod/Ship/simRun/Sim/initialization.py~ b/src/Mod/Ship/simRun/Sim/initialization.py~ new file mode 100644 index 000000000..63cc9f3aa --- /dev/null +++ b/src/Mod/Ship/simRun/Sim/initialization.py~ @@ -0,0 +1,117 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# Simulation stuff +from Utils import * + +# pyOpenCL +import pyopencl as cl +import numpy as np + +class perform: + def __init__(self, context, queue, FSmesh, waves): + """ Constructor, includes program loading. + @param context OpenCL context where apply. + @param queue OpenCL command queue. + @param FSmesh Initial free surface mesh. + @param waves Considered simulation waves (A,T,phi,heading). + """ + self.context = context + self.queue = queue + self.program = loadProgram(context, clPath() + "/simInit.cl") + self.loadData(FSmesh, waves) + self.execute() + + def loadData(self, FSmesh, waves): + """ Convert data to numpy format, and create OpenCL + buffers. + @param FSmesh Initial free surface mesh. + @param waves Considered simulation waves (A,T,phi,heading). + """ + mf = cl.mem_flags + nx = len(FSmesh) + ny = len(FSmesh[0]) + nW = len(waves) + # Mesh data + p = np.ndarray((nx*ny, 4), dtype=np.float32) + n = np.ndarray((nx*ny, 4), dtype=np.float32) + a = np.ndarray((nx*ny, 1), dtype=np.float32) + for i in range(0, nx): + for j in range(0, ny): + id = i*ny + j + pos = FSmesh[i][j].pos + normal = FSmesh[i][j].normal + area = FSmesh[i][j].area + p[id,0] = pos.x + p[id,1] = pos.y + p[id,2] = pos.z + p[id,3] = 1. + n[id,0] = normal.x + n[id,1] = normal.y + n[id,2] = normal.z + n[id,3] = 0. + a[id,0] = area + p_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=p) + n_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=n) + a_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=a) + v_cl = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny*4 * np.dtype('float32').itemsize) + f_cl = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny*4 * np.dtype('float32').itemsize) + phi = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny * np.dtype('float32').itemsize) + Phi = cl.Buffer(self.context, mf.READ_WRITE, size = nx*ny * np.dtype('float32').itemsize) + self.fs = {'Nx':nx, 'Ny':ny, 'pos':p_cl, 'vel':v_cl, 'acc':f_cl, \ + 'normal':n_cl, 'area':a_cl, 'velPot':phi, 'accPot':Phi} + # Waves data + w = np.ndarray((nW, 4), dtype=np.float32) + for i in range(0,nW): + w[i,0] = waves[i][0] + w[i,1] = waves[i][1] + w[i,2] = waves[i][2] + w[i,3] = waves[i][3] + w_cl = cl.Buffer(self.context, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=w) + self.waves = {'N':nW, 'data':w_cl} + # Ensure that all data has been written + self.queue.finish() + + def execute(self): + """ Compute initial conditions. """ + # Global size computation + N = np.ndarray((2, 1), dtype=np.uint32) + N[0] = self.fs['Nx'] + N[1] = self.fs['Ny'] + n = np.uint32(self.waves['N']) + gSize = (globalSize(N[0]),globalSize(N[1]),) + print(gSize) + # Kernel arguments + kernelargs = (self.fs['pos'], + self.fs['vel'], + self.fs['acc'], + self.waves['data'], + self.fs['velPot'], + self.fs['accPot'], + N, n) + print('Launching...') + # Kernel launch + self.program.FS(self.queue, gSize, None, *(kernelargs)) + print('Waiting...') + self.queue.finish() + print('OK!') diff --git a/src/Mod/Ship/simRun/Simulation.py b/src/Mod/Ship/simRun/Simulation.py index 32b570757..6ffd9fe0c 100644 --- a/src/Mod/Ship/simRun/Simulation.py +++ b/src/Mod/Ship/simRun/Simulation.py @@ -1,97 +1,105 @@ -#*************************************************************************** -#* * -#* Copyright (c) 2011, 2012 * -#* Jose Luis Cercos Pita * -#* * -#* This program is free software; you can redistribute it and/or modify * -#* it under the terms of the GNU Lesser General Public License (LGPL) * -#* as published by the Free Software Foundation; either version 2 of * -#* the License, or (at your option) any later version. * -#* for detail see the LICENCE text file. * -#* * -#* This program is distributed in the hope that it will be useful, * -#* but WITHOUT ANY WARRANTY; without even the implied warranty of * -#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -#* GNU Library General Public License for more details. * -#* * -#* You should have received a copy of the GNU Library General Public * -#* License along with this program; if not, write to the Free Software * -#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * -#* USA * -#* * -#*************************************************************************** - -import time -from math import * -import threading - -# pyOpenCL -import pyopencl as cl -import numpy as np - -# FreeCAD -import FreeCAD,FreeCADGui -from FreeCAD import Base, Vector -import Part - -# Ship design module -from shipUtils import Paths, Translator, Math - -class Singleton(type): - def __init__(cls, name, bases, dct): - cls.__instance = None - type.__init__(cls, name, bases, dct) - - def __call__(cls, *args, **kw): - if cls.__instance is None: - cls.__instance = type.__call__(cls, *args,**kw) - return cls.__instance - -class FreeCADShipSimulation(threading.Thread): - __metaclass__ = Singleton - def __init__ (self, device, endTime, output, FSmesh, waves): - """ Thread constructor. - @param device Device to use. - @param endTime Maximum simulation time. - @param output [Rate,Type] Output rate, Type=0 if FPS, 1 if IPF. - @param FSmesh Free surface mesh faces. - @param waves Waves parameters (A,T,phi,heading) - """ - threading.Thread.__init__(self) - # Setup as stopped - self.active = False - # Build OpenCL context and command queue - self.device = device - self.context = cl.Context(devices=[self.device]) - self.queue = cl.CommandQueue(self.context) - # Storage data - self.endTime = endTime - self.output = output - self.FSmesh = FSmesh - self.waves = waves - - def run(self): - """ Runs the simulation. - """ - self.active = True - # Perform work here - while self.active: - print("Im thread, Im running...") - time.sleep(1) - # ... - print("Im thread, step done!") - # Set thread as stopped (and prepare it to restarting) - self.active = False - threading.Event().set() - threading.Thread.__init__(self) - - def stop(self): - """ Call to stop execution. - """ - self.active = False - - def isRunning(self): - """ Report thread state - @return True if thread is running, False otherwise. - """ - return self.active +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import time +from math import * +import threading + +# pyOpenCL +import pyopencl as cl +import numpy as np + +# FreeCAD +import FreeCAD,FreeCADGui +from FreeCAD import Base, Vector +import Part + +# Simulation stuff +from Sim import initialization + +# Ship design module +from shipUtils import Paths, Translator, Math + +class Singleton(type): + def __init__(cls, name, bases, dct): + cls.__instance = None + type.__init__(cls, name, bases, dct) + + def __call__(cls, *args, **kw): + if cls.__instance is None: + cls.__instance = type.__call__(cls, *args,**kw) + return cls.__instance + +class FreeCADShipSimulation(threading.Thread): + __metaclass__ = Singleton + def __init__ (self, device, endTime, output, FSmesh, waves): + """ Thread constructor. + @param device Device to use. + @param endTime Maximum simulation time. + @param output [Rate,Type] Output rate, Type=0 if FPS, 1 if IPF. + @param FSmesh Free surface mesh faces. + @param waves Waves parameters (A,T,phi,heading) + """ + threading.Thread.__init__(self) + # Setup as stopped + self.active = False + # Build OpenCL context and command queue + self.device = device + self.context = cl.Context(devices=[self.device]) + self.queue = cl.CommandQueue(self.context) + # Storage data + self.endTime = endTime + self.output = output + self.FSmesh = FSmesh + self.waves = waves + + def run(self): + """ Runs the simulation. + """ + self.active = True + # Perform work here + msg = Translator.translate("\t[Sim]: Initializating OpenCL...\n") + FreeCAD.Console.PrintMessage(msg) + init = initialization.perform(self.context,self.queue,self.FSmesh,self.waves) + msg = Translator.translate("\t[Sim]: Iterating (outputs will be noticed)...\n") + FreeCAD.Console.PrintMessage(msg) + while self.active: + print("Im thread, Im running...") + time.sleep(1) + # ... + print("Im thread, step done!") + # Set thread as stopped (and prepare it to restarting) + self.active = False + threading.Event().set() + threading.Thread.__init__(self) + + def stop(self): + """ Call to stop execution. + """ + self.active = False + + def isRunning(self): + """ Report thread state + @return True if thread is running, False otherwise. + """ + return self.active diff --git a/src/Mod/Ship/simRun/Simulation.py~ b/src/Mod/Ship/simRun/Simulation.py~ new file mode 100644 index 000000000..03a443779 --- /dev/null +++ b/src/Mod/Ship/simRun/Simulation.py~ @@ -0,0 +1,107 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import time +from math import * +import threading + +# pyOpenCL +import pyopencl as cl +import numpy as np + +# FreeCAD +import FreeCAD,FreeCADGui +from FreeCAD import Base, Vector +import Part + +# Simulation stuff +from Sim import initialization + +# Ship design module +from shipUtils import Paths, Translator, Math + +class Singleton(type): + def __init__(cls, name, bases, dct): + cls.__instance = None + type.__init__(cls, name, bases, dct) + + def __call__(cls, *args, **kw): + if cls.__instance is None: + cls.__instance = type.__call__(cls, *args,**kw) + return cls.__instance + +class FreeCADShipSimulation(threading.Thread): + __metaclass__ = Singleton + def __init__ (self, device, endTime, output, FSmesh, waves): + """ Thread constructor. + @param device Device to use. + @param endTime Maximum simulation time. + @param output [Rate,Type] Output rate, Type=0 if FPS, 1 if IPF. + @param FSmesh Free surface mesh faces. + @param waves Waves parameters (A,T,phi,heading) + """ + threading.Thread.__init__(self) + # Setup as stopped + self.active = False + # Build OpenCL context and command queue + self.device = device + self.context = cl.Context(devices=[self.device]) + self.queue = cl.CommandQueue(self.context) + # Storage data + self.endTime = endTime + self.output = output + self.FSmesh = FSmesh + self.waves = waves + + def run(self): + """ Runs the simulation. + """ + self.active = True + # Perform work here + msg = Translator.translate("\t[Sim]: Initializating OpenCL...\n") + FreeCAD.Console.PrintMessage(msg) + init = initialization.perform(self.context,self.queue,self.FSmesh,self.waves) + msg = Translator.translate("\t[Sim]: Iterating (outputs will be noticed)...\n") + FreeCAD.Console.PrintMessage(msg) + while self.active: + print("Im thread, Im running...") + time.sleep(1) + # ... + print("Im thread, step done!") + msg = Translator.translate("\t[Sim]: Output performed!\n") + FreeCAD.Console.PrintMessage(msg) + # Set thread as stopped (and prepare it to restarting) + self.active = False + threading.Event().set() + threading.Thread.__init__(self) + + def stop(self): + """ Call to stop execution. + """ + self.active = False + + def isRunning(self): + """ Report thread state + @return True if thread is running, False otherwise. + """ + return self.active