Skip to content

Commit bbcbe23

Browse files
author
Ash Setter
committed
added hello world in c++ called from python and the same with OpenMP for multithreading - avoiding the Global Interpreter Lock through use of C++
0 parents  commit bbcbe23

9 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎HelloWorld/helloworld.pyx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cdef extern from "helloworldC.cpp":
2+
void Helloworld() # cpdef instructs cython to automatically wrap the C function with a python function
3+
4+
def C_Helloworld():
5+
Helloworld()
6+
7+
#c_helloworld()

‎HelloWorld/helloworldC.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <iostream>
2+
3+
void Helloworld(){
4+
std::cout << "Hello world!" << "\n";
5+
}

‎HelloWorld/makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
python setup.py build_ext --inplace

‎HelloWorld/minimalRunScript.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import helloworld
2+
3+
helloworld.C_Helloworld()

‎HelloWorld/setup.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from distutils.core import setup
2+
from distutils.extension import Extension
3+
from Cython.Build import cythonize
4+
5+
ext = Extension('helloworld', sources=["helloworld.pyx"], language="c++")
6+
7+
setup(name="helloworld", ext_modules = cythonize([ext]))

‎HelloWorldOpenMP/helloworld.pyx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cdef extern from "helloworldC.cpp":
2+
void Helloworld() # cpdef instructs cython to automatically wrap the C function with a python function
3+
4+
def C_Helloworld():
5+
Helloworld()
6+
7+
#c_helloworld()

‎HelloWorldOpenMP/helloworldC.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include <omp.h>
3+
4+
void Helloworld(){
5+
#pragma omp parallel
6+
{
7+
int Thread_Num = omp_get_thread_num();
8+
#pragma omp critical
9+
std::cout << "Hello world! from Thread " << Thread_Num << "\n";
10+
}
11+
}

‎HelloWorldOpenMP/makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
python setup.py build_ext --inplace

‎HelloWorldOpenMP/setup.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from distutils.core import setup
2+
from distutils.extension import Extension
3+
from Cython.Build import cythonize
4+
5+
ext = Extension('helloworld', sources=["helloworld.pyx"], language="c++", extra_compile_args=['-fopenmp'], extra_link_args=['-lgomp'])
6+
7+
setup(name="helloworld", ext_modules = cythonize([ext]))

0 commit comments

Comments
 (0)