Esempio n. 1
0
class ParticleSystem2d(list):
    """system of particles, contains the array's that will be crunched during simulation"""
    def __init__(self,n_particles):
        self.masses = np.ones([n_particles],dtype=float)*1.0
        self.positions = np.zeros([n_particles,2],dtype='float64')
        self.forces = np.zeros([n_particles,2],dtype='float64')
        self.velocities = np.zeros([n_particles,2],dtype='float64')
        self.accelerations = np.zeros([n_particles,2],dtype='float64')
        self.extend([SystemParticle2d(system = self,
                                      index = i,
                                      position = [np.random.random()*1000,np.random.random()*1000],
                                      mass=13) for i in range(n_particles)])
        self.touched = True
        try:
            import opencl as cl
            from clyther.array import CLArrayContext
            self.ca = CLArrayContext()
            print np.shape(self.masses)
            print np.shape(self.forces)
            print np.shape(self.positions)
            print np.shape(self.velocities)
            self.CLarray = self.ca.asarray(np.vstack((self.masses,self.forces.T,self.positions.T,self.velocities.T)))
            #self.CLarray = self.ca.a
            self.positionsCL = self.ca.asarray(self.positions)
            self.velocitiesCL = self.ca.asarray(self.velocities)
            self.forcesCL = self.ca.asarray(self.forces)
            self.massesCL = self.ca.asarray(self.masses)
        except ImportError:
            pass

    def update(self):
        if self.ca:
            if self.touched:
                self.CLarray = self.ca.asarray(np.vstack((self.masses,self.forces.T,self.positions.T,self.velocities.T)))
                #print 'here'
                #self.CLarray[px_ind:py_ind+1,:] = np.array(self.positions[:,:].T)
                #self.velocitiesCL = self.ca.asarray(self.velocities)
                #self.forcesCL = self.ca.asarray(self.forces)
            import opencl as cl
            queue = cl.Queue(self.ca)
            clcall = compute_gravitation(queue,self.CLarray)
            clcall.wait()
            self.CLarray[px_ind,:] += self.CLarray[vx_ind,:]*0.1
            self.CLarray[py_ind,:] += self.CLarray[vy_ind,:]*0.1
            self.CLarray[vx_ind,:] += self.CLarray[fx_ind,:]* (1/1.0)*0.1
            self.CLarray[vy_ind,:] += self.CLarray[fy_ind,:]* (1/1.0)*0.1
            with self.CLarray.map() as arr:
                self.positions[:] = arr[px_ind:py_ind+1,:].T
                self.velocities[:] = arr[vx_ind:vy_ind+1,:].T
                self.forces[:] =  arr[fx_ind:fy_ind+1,:].T
            #with self.velocitiesCL.map() as arr:
            #    self.velocities[:] = arr[:]
        else:
            print 'here'
            self.positions += self.velocities*0.1
            self.velocities += self.forces* (1/13.0)*0.1
        #print id(self.positions)

    def plot(self):
        [p.plot() for p in self]
Esempio n. 2
0
def main():
    ca = CLArrayContext()  
    
    a = ca.empty([1], ctype='f')
    
    foo = Foo(10., 2.)
     
    objects(ca, a, foo)
    
    print "compiled result: ", a.item().value
    print "python result:   ", foo.bar()
Esempio n. 3
0
def main():
    ca = CLArrayContext()

    a = ca.empty([1], ctype='f')

    foo = Foo(10., 2.)

    objects(ca, a, foo)

    print "compiled result: ", a.item().value
    print "python result:   ", foo.bar()
Esempio n. 4
0
def main():

    ca = CLArrayContext(device_type=cl.Device.GPU)

    size = 8

    device_input = ca.arange(size)

    output = ca.add.reduce(device_input)

    print output.item()
    with output.map() as view:
        print "device sum", np.asarray(view).item()
Esempio n. 5
0
def main():
    
    ca = CLArrayContext(device_type=cl.Device.GPU)
    
    size = 8
    
    device_input = ca.arange(size)
    
    output = ca.add.reduce(device_input)
    
    print output.item()
    with output.map() as view:
        print "device sum", np.asarray(view).item()
Esempio n. 6
0
 def __init__(self,n_particles):
     self.masses = np.ones([n_particles],dtype=float)*1.0
     self.positions = np.zeros([n_particles,2],dtype='float64')
     self.forces = np.zeros([n_particles,2],dtype='float64')
     self.velocities = np.zeros([n_particles,2],dtype='float64')
     self.accelerations = np.zeros([n_particles,2],dtype='float64')
     self.extend([SystemParticle2d(system = self,
                                   index = i,
                                   position = [np.random.random()*1000,np.random.random()*1000],
                                   mass=13) for i in range(n_particles)])
     self.touched = True
     try:
         import opencl as cl
         from clyther.array import CLArrayContext
         self.ca = CLArrayContext()
         print np.shape(self.masses)
         print np.shape(self.forces)
         print np.shape(self.positions)
         print np.shape(self.velocities)
         self.CLarray = self.ca.asarray(np.vstack((self.masses,self.forces.T,self.positions.T,self.velocities.T)))
         #self.CLarray = self.ca.a
         self.positionsCL = self.ca.asarray(self.positions)
         self.velocitiesCL = self.ca.asarray(self.velocities)
         self.forcesCL = self.ca.asarray(self.forces)
         self.massesCL = self.ca.asarray(self.masses)
     except ImportError:
         pass
Esempio n. 7
0
def setUpModule():
    global ca

    DEVICE_TYPE_ATTR = os.environ.get('DEVICE_TYPE', 'DEFAULT')
    DEVICE_TYPE = getattr(cl.Device, DEVICE_TYPE_ATTR)

    ca = CLArrayContext(device_type=DEVICE_TYPE)

    print ca.devices
Esempio n. 8
0
"""
Created on Dec 15, 2011

@author: sean
"""

import opencl as cl
from clyther.array import CLArrayContext

# Always have to create a context.
ca = CLArrayContext()

# can print the current devices
print ca.devices

# Create an array
a = ca.arange(12)

print a

# map is the same as a memory map
with a.map() as arr:
    print arr

# can clice
b = a[1::2]

with b.map() as arr:
    print arr

# ufuncs
Esempio n. 9
0
'''
Created on Dec 15, 2011

@author: sean
'''

import opencl as cl
from clyther.array import CLArrayContext
#Always have to create a context.
ca = CLArrayContext()

#can print the current devices
print ca.devices

#Create an array
a = ca.arange(12)

print a

#map is the same as a memory map
with a.map() as arr:
    print arr

#can clice
b = a[1::2]

with b.map() as arr:
    print arr

#ufuncs
c = a + 1