Révision 127 Pi/XPU/PiXpuThreads.py

PiXpuThreads.py (revision 127)
1
#!/usr/bin/env python
1
#!/usr/bin/env python3
2 2

  
3 3
#
4 4
# Pi-by-MonteCarlo using PyCUDA/PyOpenCL
......
33 33
class threadWithReturn(Thread):
34 34
    def __init__(self, *args, **kwargs):
35 35
        super(threadWithReturn, self).__init__(*args, **kwargs)
36

  
37 36
        self._return = None
38 37

  
39 38
    def run(self):
40
        if self._Thread__target is not None:
41
            self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
39
        if self._target is not None:
40
            self._return = self._target(*self._args, **self._kwargs)
42 41

  
43 42
    def join(self, *args, **kwargs):
44 43
        super(threadWithReturn, self).join(*args, **kwargs)
45

  
46 44
        return self._return
47 45

  
48 46
if __name__=='__main__':
......
86 84
    try:
87 85
        opts, args = getopt.getopt(sys.argv[1:],"hocg:i:b:e:s:f:l:t:r:d:m:v:",["gpustyle=","iterations=","blocksBegin=","blocksEnd=","blocksStep=","threadsFirst=","threadsLast=","threadssTep=","redo=","device=","marsaglia=","valuetype="])
88 86
    except getopt.GetoptError:
89
        print HowToUse % sys.argv[0]
87
        print(HowToUse % sys.argv[0])
90 88
        sys.exit(2)
91 89

  
92 90
    # List of Devices
......
95 93
        
96 94
    for opt, arg in opts:
97 95
        if opt == '-h':
98
            print HowToUse % sys.argv[0]
96
            print(HowToUse % sys.argv[0])
99 97

  
100
            print "\nInformations about devices detected under OpenCL:"
98
            print("\nInformations about devices detected under OpenCL:")
101 99
            # For PyOpenCL import
102 100
            try:
103 101
                import pyopencl as cl
......
105 103
                for platform in cl.get_platforms():
106 104
                    for device in platform.get_devices():
107 105
                        deviceType=cl.device_type.to_string(device.type)
108
                        print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip())
106
                        print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip(),deviceType,device.name.lstrip()))
109 107
                        Id=Id+1
110 108

  
111 109
                print
112 110
                sys.exit()
113 111
            except ImportError:
114
                print "Your platform does not seem to support OpenCL"
112
                print("Your platform does not seem to support OpenCL")
115 113
        
116 114
        elif opt == '-o':
117 115
            OutMetrology=True
......
143 141
        elif opt in ("-r", "--redo"):
144 142
            Redo = int(arg)
145 143

  
146
    print "Devices Identification : %s" % Devices
147
    print "GpuStyle used : %s" % GpuStyle
148
    print "Iterations : %s" % Iterations
149
    print "Number of Blocks on begin : %s" % BlocksBegin
150
    print "Number of Blocks on end : %s" % BlocksEnd
151
    print "Step on Blocks : %s" % BlocksStep
152
    print "Number of Threads on begin : %s" % ThreadsBegin
153
    print "Number of Threads on end : %s" % ThreadsEnd
154
    print "Step on Threads : %s" % ThreadsStep
155
    print "Number of redo : %s" % Redo
156
    print "Metrology done out of XPU : %r" % OutMetrology
157
    print "Type of Marsaglia RNG used : %s" % RNG
158
    print "Type of variable : %s" % ValueType
144
    print("Devices Identification : %s" % Devices)
145
    print("GpuStyle used : %s" % GpuStyle)
146
    print("Iterations : %s" % Iterations)
147
    print("Number of Blocks on begin : %s" % BlocksBegin)
148
    print("Number of Blocks on end : %s" % BlocksEnd)
149
    print("Step on Blocks : %s" % BlocksStep)
150
    print("Number of Threads on begin : %s" % ThreadsBegin)
151
    print("Number of Threads on end : %s" % ThreadsEnd)
152
    print("Step on Threads : %s" % ThreadsStep)
153
    print("Number of redo : %s" % Redo)
154
    print("Metrology done out of XPU : %r" % OutMetrology)
155
    print("Type of Marsaglia RNG used : %s" % RNG)
156
    print("Type of variable : %s" % ValueType)
159 157

  
160 158
    if GpuStyle=='CUDA':
161 159
        try:
......
165 163
            import pycuda.autoinit
166 164
            from pycuda.compiler import SourceModule
167 165
        except ImportError:
168
            print "Platform does not seem to support CUDA"
166
            print("Platform does not seem to support CUDA")
169 167

  
170 168
    if GpuStyle=='OpenCL':
171 169
        try:
......
175 173
            for platform in cl.get_platforms():
176 174
                for device in platform.get_devices():
177 175
                    deviceType=cl.device_type.to_string(device.type)
178
                    print "Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip())
176
                    print("Device #%i from %s of type %s : %s" % (Id,platform.vendor.lstrip().rstrip(),deviceType,device.name.lstrip().rstrip()))
179 177

  
180 178
                    if Id in Devices:
181 179
                    # Set the Alu as detected Device Type
182 180
                        Alu[Id]=deviceType
183 181
                    Id=Id+1
184 182
        except ImportError:
185
            print "Platform does not seem to support OpenCL"
183
            print("Platform does not seem to support OpenCL")
186 184

  
187
    print Devices,Alu
185
    print(Devices,Alu)
188 186
            
189
    BlocksList=xrange(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
190
    ThreadsList=xrange(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
187
    BlocksList=range(BlocksBegin,BlocksEnd+BlocksStep,BlocksStep)
188
    ThreadsList=range(ThreadsBegin,ThreadsEnd+ThreadsStep,ThreadsStep)
191 189
    
192 190
    ExploredJobs=numpy.array([]).astype(numpy.uint32)
193 191
    ExploredBlocks=numpy.array([]).astype(numpy.uint32)
......
234 232
                        try:
235 233
                            MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,))
236 234
                        except:
237
                            print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
235
                            print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
238 236
                    elif GpuStyle=='OpenCL':
239 237
                        try:
240 238
                            MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,))
241 239
                        except:
242
                            print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
240
                            print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            )
243 241
                    
244
                    print "Start on #%i device..." % Device
242
                    print("Start on #%i device..." % Device)
245 243
                    MyThread.start()
246 244
                    MyThreads.append(MyThread)
247 245

  
......
251 249
                    OutputCL=MyThread.join()
252 250
                    NewIterations+=OutputCL['NewIterations']
253 251
                    Inside+=OutputCL['Inside']
254
                print "Pi equals:",4./NewIterations*Inside
252
                print("Pi equals:",4./NewIterations*Inside)
255 253

  
256 254
                Duration=numpy.append(Duration,time.time()-time_start)
257 255
                Rate=numpy.append(Rate,NewIterations/Duration[-1])
......
271 269
                    try:
272 270
                        MyThread=threadWithReturn(target=MetropolisCUDA, args=(InputCL,))
273 271
                    except:
274
                        print "Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads)
272
                        print("Problem with (%i,%i) // computations on Cuda" % (Blocks,Threads))
275 273
                elif GpuStyle=='OpenCL':
276 274
                    try:
277 275
                        MyThread=threadWithReturn(target=MetropolisOpenCL, args=(InputCL,))
278 276
                    except:
279
                        print "Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads)            
277
                        print("Problem with (%i,%i) // computations on OpenCL" % (Blocks,Threads))
280 278
                    
281
                print "Start on #%i device..." % Device
279
                print("Start on #%i device..." % Device)
282 280
                MyThread.start()
283 281
                MyThreads.append(MyThread)
284 282

  
......
289 287
                NewIterations+=OutputCL['NewIterations']
290 288
                Inside+=OutputCL['Inside']
291 289
            Duration=time.time()-time_start
292
            print "Pi equals:",4./NewIterations*Inside
290
            print("Pi equals:",4./NewIterations*Inside)
293 291
            Rate=1./Duration*NewIterations
294 292
            
295 293
        avgD=numpy.append(avgD,numpy.average(Duration))
......
303 301
        minR=numpy.append(minR,numpy.min(Rate))
304 302
        maxR=numpy.append(maxR,numpy.max(Rate))
305 303

  
306
        print "%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1])
304
        print("%.2f %.2f %.2f %.2f %.2f %i %i %i %i %i" % (avgD[-1],medD[-1],stdD[-1],minD[-1],maxD[-1],avgR[-1],medR[-1],stdR[-1],minR[-1],maxR[-1]))
307 305
        
308 306
        numpy.savez("Pi_%s_%s_%s_%s_%s_%s_%s_%s_%.8i_Device%i_%s_%s" % (ValueType,RNG,Alu[Devices[0]],GpuStyle,BlocksBegin,BlocksEnd,ThreadsBegin,ThreadsEnd,Iterations,Devices[0],Metrology,gethostname()),(ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR))
309 307
        ToSave=[ ExploredBlocks,ExploredThreads,avgD,medD,stdD,minD,maxD,avgR,medR,stdR,minR,maxR ]

Formats disponibles : Unified diff