Coverage for ensae_teaching_dl/faq/faq_gpu.py: 24%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

34 statements  

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief A few functions about :epkg:`GPU`. 

5""" 

6 

7 

8def pyopencl_status(): 

9 """ 

10 Looks into :epkg:`GPU` and :epkg:`CPU` to 

11 see which card is available. 

12 Returns a string. 

13 

14 .. index:: GPU 

15 

16 .. runpython:: 

17 :showcode: 

18 

19 from ensae_teaching_dl.faq.faq_gpu import pyopencl_status 

20 print(pyopencl_status()) 

21 """ 

22 rows = [] 

23 try: 

24 import pyopencl as cl 

25 except ImportError as e: 

26 rows.append("pyopencl is not available due to {}".format(e)) 

27 return "\n".join(rows) 

28 

29 def catch(fct): 

30 try: 

31 return fct() 

32 except Exception as e: # pylint: disable=W0703 

33 return "Unable to retrieve that information due to {}".format(str(e).replace("\n", " ")) 

34 

35 rows.append('=' * 60) 

36 rows.append('OpenCL plats and Devices') 

37 for plat in cl.get_platforms(): 

38 rows.append('=' * 60) 

39 rows.append('plat - Name: ' + plat.name) 

40 rows.append('plat - Vendor: ' + plat.vendor) 

41 rows.append('plat - Version: ' + plat.version) 

42 rows.append('plat - Profile: ' + plat.profile) 

43 # rows.append each device per-plat 

44 for device in plat.get_devices(): 

45 rows.append('-' * 56) 

46 rows.append('Device - Name: ' + 

47 catch(lambda: device.name)) # pylint: disable=W0640 

48 rows.append('Device - Type: {}'.format( 

49 catch(lambda: cl.device_type.to_string(device.type)))) # pylint: disable=W0640 

50 rows.append('Device - Max Clock Speed: {0} Mhz'.format( 

51 catch(lambda: device.max_clock_frequency))) # pylint: disable=W0640 

52 rows.append('Device - Compute Units: {0}'.format( 

53 catch(lambda: device.max_compute_units))) # pylint: disable=W0640 

54 rows.append('Device - Local Memory: {0:.0f} KB'.format( 

55 catch(lambda: device.local_mem_size / 1024.0))) # pylint: disable=W0640 

56 rows.append('Device - Constant Memory: {0:.0f} KB'.format( 

57 catch(lambda: device.max_constant_buffer_size / 1024.0))) # pylint: disable=W0640 

58 rows.append('Device - Global Memory: {0:.0f} GB'.format( 

59 catch(lambda: device.global_mem_size / 1073741824.0))) # pylint: disable=W0640 

60 rows.append('Device - Max Buffer/Image Size: {0:.0f} MB'.format( 

61 catch(lambda: device.max_mem_alloc_size / 1048576.0))) # pylint: disable=W0640 

62 rows.append('Device - Max Work Group Size: {0:.0f}'.format( 

63 catch(lambda: device.max_work_group_size))) # pylint: disable=W0640 

64 rows.append('=' * 60) 

65 return "\n".join(rows)