Advertisement
Guest User

Untitled

a guest
Dec 9th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. ================== omptest.c ==================
  2. #include <omp.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #ifdef PYTHON_MODULE
  6. #include <Python.h>
  7. #endif
  8.  
  9. static unsigned short *a = NULL;
  10. static unsigned short *b = NULL;
  11. static unsigned short *c = NULL;
  12. static double *r = NULL;
  13. static int length = 0;
  14.  
  15. static void init_test(int len) {
  16. int i;
  17. if (length)
  18. return;
  19. length = len;
  20. a = (unsigned short*)calloc(length, sizeof(unsigned short));
  21. b = (unsigned short*)calloc(length, sizeof(unsigned short));
  22. c = (unsigned short*)calloc(length, sizeof(unsigned short));
  23. r = (double*)calloc(length, sizeof(double));
  24. for (i = 0; i < length; i++) {
  25. a[i] = (unsigned short)(((i + 13) * 4001) % 2017);
  26. b[i] = (unsigned short)(((i + 17) * 4001) % 2017);
  27. c[i] = (unsigned short)(((i + 19) * 4001) % 2017);
  28. }
  29. }
  30.  
  31. static int do_test(int use_omp, int count) {
  32. int i;
  33. int threads = 1;
  34. if (use_omp) {
  35. #ifdef PYTHON_MODULE
  36. Py_BEGIN_ALLOW_THREADS
  37. #endif
  38. threads = omp_get_max_threads();
  39. omp_set_num_threads(threads);
  40. while (count--) {
  41. #pragma omp parallel for private(i)
  42. for (i = 0; i < length; i++)
  43. r[i] = (double)a[i] / pow((double)b[i] + 1.0, 2) +
  44. sqrt(fabs((double)c[i]));
  45. }
  46. #ifdef PYTHON_MODULE
  47. Py_END_ALLOW_THREADS
  48. #endif
  49. }
  50. else {
  51. while (count--) {
  52. for (i = 0; i < length; i++)
  53. r[i] = (double)a[i] / pow((double)b[i] + 1.0, 2) +
  54. sqrt(fabs((double)c[i]));
  55. }
  56. }
  57. return threads;
  58. }
  59.  
  60. #ifdef PYTHON_MODULE
  61. static PyObject *omptest_init(PyObject *self, PyObject *args) {
  62. int length;
  63. if (!PyArg_ParseTuple(args, "i", &length))
  64. return NULL;
  65. init_test(length);
  66. Py_RETURN_NONE;
  67. }
  68.  
  69. static PyObject *omptest_do(PyObject *self, PyObject *args) {
  70. int use_omp, count;
  71. int threads;
  72. if (!PyArg_ParseTuple(args, "ii", &use_omp, &count))
  73. return NULL;
  74. threads = do_test(use_omp, count);
  75. return Py_BuildValue("i", threads);
  76. }
  77. static PyMethodDef OMPTestMethods[] = {
  78. {"init", omptest_init, METH_VARARGS, ""},
  79. {"do", omptest_do, METH_VARARGS, ""},
  80. {NULL, NULL, 0, NULL}
  81. };
  82.  
  83. #if PY_MAJOR_VERSION >= 3
  84. static struct PyModuleDef omptestmodule = {
  85. PyModuleDef_HEAD_INIT,
  86. "omptest",
  87. "",
  88. -1,
  89. OMPTestMethods
  90. };
  91. PyMODINIT_FUNC PyInit_omptest(void)
  92. {
  93. PyEval_InitThreads();
  94. return PyModule_Create(&omptestmodule);
  95. }
  96. #else
  97. PyMODINIT_FUNC initomptest(void)
  98. {
  99. PyEval_InitThreads();
  100. (void) Py_InitModule("omptest", OMPTestMethods);
  101. }
  102. #endif
  103. #else
  104. #include <stdio.h>
  105. #include <sys/time.h>
  106. #include <time.h>
  107. int main() {
  108. int use_omp, threads;
  109. struct timeval tv;
  110. double t1, t2;
  111. init_test(1024*1024);
  112. for (use_omp = 0; use_omp <= 1; use_omp++) {
  113. gettimeofday(&tv, NULL);
  114. t1 = (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
  115. threads = do_test(use_omp, 100);
  116. gettimeofday(&tv, NULL);
  117. t2 = (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
  118. printf("[C] use_omp = %d, threads = %d, elapsed %.3fs\n",
  119. use_omp, threads, t2 - t1);
  120. }
  121. }
  122. #endif
  123.  
  124. ================== setup.py ==================
  125. # -*- coding: utf-8 -*-
  126.  
  127. from distutils.core import setup, Extension
  128.  
  129. module1 = Extension('omptest',
  130. sources=['omptest.c'],
  131. extra_compile_args=['-DPYTHON_MODULE', '-O4', '-fopenmp'],
  132. extra_link_args=['-lgomp', '-lm'])
  133.  
  134. setup(name='OMPTest',
  135. version='0.0.0',
  136. description='Test OpenMP withing C extension for Python',
  137. ext_modules=[module1])
  138.  
  139. ================== pyomptest.c ==================
  140. # -*- coding: utf-8 -*-
  141.  
  142. import time
  143. import omptest
  144.  
  145. omptest.init(1024*1024)
  146. for use_omp in [0, 1]:
  147. t1 = time.clock()
  148. threads = omptest.do(use_omp, 100)
  149. t2 = time.clock()
  150. print("[Py] use_omp = %d, threads = %d, elapsed %.3fs" %
  151. (use_omp, threads, t2 - t1))
  152.  
  153. ================== Makefile ==================
  154. all: comptest pyomptest
  155.  
  156. comptest: omptest.c
  157. gcc -O4 -fopenmp -lgomp -lm omptest.c -o omptest
  158. ./omptest
  159.  
  160. pyomptest: omptest.c pyomptest.py setup.py
  161. python setup.py build
  162. python setup.py install
  163. python pyomptest.py
  164.  
  165. ================== "sudo make" stdandard output ==================
  166. [C] use_omp = 0, threads = 1, elapsed 1.177s
  167. [C] use_omp = 1, threads = 4, elapsed 0.663s
  168. [Py] use_omp = 0, threads = 1, elapsed 1.171s
  169. [Py] use_omp = 1, threads = 4, elapsed 2.610s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement