Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ================== omptest.c ==================
- #include <omp.h>
- #include <stdlib.h>
- #include <math.h>
- #ifdef PYTHON_MODULE
- #include <Python.h>
- #endif
- static unsigned short *a = NULL;
- static unsigned short *b = NULL;
- static unsigned short *c = NULL;
- static double *r = NULL;
- static int length = 0;
- static void init_test(int len) {
- int i;
- if (length)
- return;
- length = len;
- a = (unsigned short*)calloc(length, sizeof(unsigned short));
- b = (unsigned short*)calloc(length, sizeof(unsigned short));
- c = (unsigned short*)calloc(length, sizeof(unsigned short));
- r = (double*)calloc(length, sizeof(double));
- for (i = 0; i < length; i++) {
- a[i] = (unsigned short)(((i + 13) * 4001) % 2017);
- b[i] = (unsigned short)(((i + 17) * 4001) % 2017);
- c[i] = (unsigned short)(((i + 19) * 4001) % 2017);
- }
- }
- static int do_test(int use_omp, int count) {
- int i;
- int threads = 1;
- if (use_omp) {
- #ifdef PYTHON_MODULE
- Py_BEGIN_ALLOW_THREADS
- #endif
- threads = omp_get_max_threads();
- omp_set_num_threads(threads);
- while (count--) {
- #pragma omp parallel for private(i)
- for (i = 0; i < length; i++)
- r[i] = (double)a[i] / pow((double)b[i] + 1.0, 2) +
- sqrt(fabs((double)c[i]));
- }
- #ifdef PYTHON_MODULE
- Py_END_ALLOW_THREADS
- #endif
- }
- else {
- while (count--) {
- for (i = 0; i < length; i++)
- r[i] = (double)a[i] / pow((double)b[i] + 1.0, 2) +
- sqrt(fabs((double)c[i]));
- }
- }
- return threads;
- }
- #ifdef PYTHON_MODULE
- static PyObject *omptest_init(PyObject *self, PyObject *args) {
- int length;
- if (!PyArg_ParseTuple(args, "i", &length))
- return NULL;
- init_test(length);
- Py_RETURN_NONE;
- }
- static PyObject *omptest_do(PyObject *self, PyObject *args) {
- int use_omp, count;
- int threads;
- if (!PyArg_ParseTuple(args, "ii", &use_omp, &count))
- return NULL;
- threads = do_test(use_omp, count);
- return Py_BuildValue("i", threads);
- }
- static PyMethodDef OMPTestMethods[] = {
- {"init", omptest_init, METH_VARARGS, ""},
- {"do", omptest_do, METH_VARARGS, ""},
- {NULL, NULL, 0, NULL}
- };
- #if PY_MAJOR_VERSION >= 3
- static struct PyModuleDef omptestmodule = {
- PyModuleDef_HEAD_INIT,
- "omptest",
- "",
- -1,
- OMPTestMethods
- };
- PyMODINIT_FUNC PyInit_omptest(void)
- {
- PyEval_InitThreads();
- return PyModule_Create(&omptestmodule);
- }
- #else
- PyMODINIT_FUNC initomptest(void)
- {
- PyEval_InitThreads();
- (void) Py_InitModule("omptest", OMPTestMethods);
- }
- #endif
- #else
- #include <stdio.h>
- #include <sys/time.h>
- #include <time.h>
- int main() {
- int use_omp, threads;
- struct timeval tv;
- double t1, t2;
- init_test(1024*1024);
- for (use_omp = 0; use_omp <= 1; use_omp++) {
- gettimeofday(&tv, NULL);
- t1 = (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
- threads = do_test(use_omp, 100);
- gettimeofday(&tv, NULL);
- t2 = (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
- printf("[C] use_omp = %d, threads = %d, elapsed %.3fs\n",
- use_omp, threads, t2 - t1);
- }
- }
- #endif
- ================== setup.py ==================
- # -*- coding: utf-8 -*-
- from distutils.core import setup, Extension
- module1 = Extension('omptest',
- sources=['omptest.c'],
- extra_compile_args=['-DPYTHON_MODULE', '-O4', '-fopenmp'],
- extra_link_args=['-lgomp', '-lm'])
- setup(name='OMPTest',
- version='0.0.0',
- description='Test OpenMP withing C extension for Python',
- ext_modules=[module1])
- ================== pyomptest.c ==================
- # -*- coding: utf-8 -*-
- import time
- import omptest
- omptest.init(1024*1024)
- for use_omp in [0, 1]:
- t1 = time.clock()
- threads = omptest.do(use_omp, 100)
- t2 = time.clock()
- print("[Py] use_omp = %d, threads = %d, elapsed %.3fs" %
- (use_omp, threads, t2 - t1))
- ================== Makefile ==================
- all: comptest pyomptest
- comptest: omptest.c
- gcc -O4 -fopenmp -lgomp -lm omptest.c -o omptest
- ./omptest
- pyomptest: omptest.c pyomptest.py setup.py
- python setup.py build
- python setup.py install
- python pyomptest.py
- ================== "sudo make" stdandard output ==================
- [C] use_omp = 0, threads = 1, elapsed 1.177s
- [C] use_omp = 1, threads = 4, elapsed 0.663s
- [Py] use_omp = 0, threads = 1, elapsed 1.171s
- [Py] use_omp = 1, threads = 4, elapsed 2.610s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement