Browse Source

Started integrating C with Python and MPI/OpenMP

Adam Kelly 5 years ago
parent
commit
685d62748b

+ 38 - 6
CMakeLists.txt

@@ -1,11 +1,43 @@
 cmake_minimum_required(VERSION 3.9)
-project(tangle)
 
-set(TANGLE_SRC_DIR "${PROJECT_SOURCE_DIR}/src")
-set(CLI_MAIN "${PROJECT_SOURCE_DIR}/extras/simulator_cli.cpp")
+project(tangle_project)
+
+# EDIT HERE 
+
+option(DISTRIBUTED "Use MPI" OFF)
+option(MULTITHREADED "Use OpenMP" ON)
+
+# END EDIT
+
+if (NOT DEFINED ${TANGLE_LIB_PATH})
+    set(TANGLE_DIR "tangle")
+    add_subdirectory(${TANGLE_DIR})
+    set(TANGLE_LIB_PATH "${CMAKE_CURRENT_BINARY_DIR}/${TANGLE_DIR}")
+    set(TANGLE_LIB_EXACT "${TANGLE_LIB_PATH}/libtangle.so")
+endif()
+
+
+# STANDALONE SIMULATOR
+
+set(USER_SOURCE "standalone/cli.c")
+set(OUTPUT_EXE "tangle-cli")
 
 if(SKBUILD) # Using Python
-    add_subdirectory(tangle)
+    add_subdirectory(python)
 else() # Using Command Line Tool
-    add_subdirectory(extras)
-endif()
+    add_executable(${OUTPUT_EXE} ${USER_SOURCE})
+    target_link_libraries(${OUTPUT_EXE} libtangle)
+
+    if (DISTRIBUTED)
+    find_package(MPI REQUIRED)
+    include_directories(${MPI_INCLUDE_PATH})
+
+    target_link_libraries(${OUTPUT_EXE} ${MPI_C_LIBRARIES})
+    add_compile_definitions(_DISTRIBUTED)
+    elseif (MULTITHREADED)
+    find_package(OpenMP REQUIRED)
+
+    target_link_libraries(${OUTPUT_EXE} OpenMP::OpenMP_C)
+    add_compile_definitions(_MULTITHREADED)
+    endif()
+endif()

+ 0 - 2
extras/CMakeLists.txt

@@ -1,2 +0,0 @@
-add_executable(simulator ${CLI_MAIN})
-set_target_properties(simulator PROPERTIES LINKER_LANGUAGE CXX CXX_STANDARD 14)

+ 0 - 7
extras/simulator_cli.cpp

@@ -1,7 +0,0 @@
-#include <iostream>
-
-int main(int argc, char const *argv[])
-{
-    std::cout << "Hello from the Simulator" << std::endl;
-    return 0;
-}

+ 15 - 0
python/CMakeLists.txt

@@ -0,0 +1,15 @@
+find_package(PythonExtensions REQUIRED)
+find_package(Cython REQUIRED)
+
+set(TANGLE_SRC_DIR "${PROJECT_SOURCE_DIR}/tangle/include")
+
+add_cython_target(_tangle.pyx C)
+add_library(_tangle MODULE ${_tangle})
+target_link_libraries(_tangle libtangle)
+# target_include_directories(_tangle PRIVATE ${TANGLE_SRC_DIR})
+python_extension_module(_tangle)
+
+message("ECO FROM MESSAAGE")
+
+
+install(TARGETS _tangle LIBRARY DESTINATION tangle)

tangle/__init__.py → python/__init__.py


+ 5 - 0
python/_tangle.pyx

@@ -0,0 +1,5 @@
+# cdef extern from "tangle.h":
+#     cdef void count_to(int i)
+
+def say_hi():
+    return print('hiaii')

+ 1 - 0
setup.py

@@ -13,6 +13,7 @@ setup(
     long_description_content_type="text/markdown",
     url="https://libtangle.com",
     packages=['tangle'],
+    package_dir={'tangle': 'python'},
     classifiers=[
         "Environment :: Console",
         "License :: OSI Approved :: MIT License",

+ 0 - 11
src/library.cpp

@@ -1,11 +0,0 @@
-#include <iostream>
-
-namespace Tangle
-{
-
-void say_hello()
-{
-    std::cout << "Pretty Tangled Here" << std::endl;
-}
-
-} // namespace Tangle

+ 32 - 0
standalone/cli.c

@@ -0,0 +1,32 @@
+#include "tangle.h"
+#include <stdio.h>
+// #include "mpi.h"
+
+int main()
+{
+    // MPI_Init(NULL, NULL);
+
+    printf("Welcome\n");
+
+    count_to(10);
+
+    // MPI_Finalize();
+
+    return 0;
+}
+
+// #include <stdio.h>
+// #include <omp.h>
+
+// int main()
+// {
+// #pragma omp parallel num_threads(3)
+//     {
+//         int id = omp_get_thread_num();
+//         int data = id;
+//         int total = omp_get_num_threads();
+//         printf("Greetings from process %d out of %d with Data %d\n", id, total, data);
+//     }
+//     printf("parallel for ends.\n");
+//     return 0;
+// }

+ 27 - 7
tangle/CMakeLists.txt

@@ -1,10 +1,30 @@
-find_package(PythonExtensions REQUIRED)
-find_package(Cython REQUIRED)
+# option(DISTRIBUTED "Use MPI" OFF)
+# option(MULTITHREADED "Use OpenMP" OFF)
 
+if (DISTRIBUTED)
+    find_package(MPI REQUIRED)
+    include_directories(${MPI_INCLUDE_PATH})
+elseif (MULTITHREADED)
+    find_package(OpenMP REQUIRED)
+endif()
 
-add_cython_target(_tangle.pyx CXX)
-add_library(_tangle MODULE ${_tangle})
-target_include_directories(_tangle PRIVATE ${TANGLE_SRC_DIR})
-python_extension_module(_tangle)
 
-install(TARGETS _tangle LIBRARY DESTINATION tangle)
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
+
+
+add_subdirectory(src)
+message("Tangle Source: ${TANGLE_SRC}")
+add_library(libtangle SHARED ${TANGLE_SRC})
+
+# Header files & linking
+target_include_directories(libtangle PRIVATE src PUBLIC include)
+
+
+if (DISTRIBUTED)
+    target_link_libraries(libtangle ${MPI_C_LIBRARIES})
+    add_compile_definitions(_DISTRIBUTED)
+elseif (MULTITHREADED)
+    target_link_libraries(libtangle PUBLIC OpenMP::OpenMP_C)
+    add_compile_definitions(_MULTITHREADED)
+
+endif()

+ 0 - 5
tangle/_tangle.pyx

@@ -1,5 +0,0 @@
-cdef extern from "library.cpp" namespace "Tangle":
-    cdef void say_hello()
-
-def say_hi():
-    return say_hello()

+ 8 - 0
tangle/include/tangle.h

@@ -0,0 +1,8 @@
+// The outward facing API for Tangle
+
+#ifndef __TANGLE_H
+#define __TANGLE_H
+
+void count_to(int i);
+
+#endif

+ 16 - 0
tangle/src/CMakeLists.txt

@@ -0,0 +1,16 @@
+if (DISTRIBUTED)
+    set(TANGLE_SRC
+        ${CMAKE_CURRENT_SOURCE_DIR}/mpi.c
+        PARENT_SCOPE
+    )
+elseif (MULTITHREADED)
+    set(TANGLE_SRC
+        ${CMAKE_CURRENT_SOURCE_DIR}/omp.c
+        PARENT_SCOPE
+    )
+else()
+    set(TANGLE_SRC
+        ${CMAKE_CURRENT_SOURCE_DIR}/cpu.c
+        PARENT_SCOPE
+    )
+endif()

+ 11 - 0
tangle/src/cpu.c

@@ -0,0 +1,11 @@
+#include "tangle.h"
+
+#include <stdio.h>
+
+void count_to(int i)
+{
+    for (int j = 1; j <= i; j++)
+    {
+        printf("%d\n", j);
+    }
+}

+ 24 - 0
tangle/src/mpi.c

@@ -0,0 +1,24 @@
+#include "tangle.h"
+
+#include <stdio.h>
+#include "mpi.h"
+
+void count_to(int i)
+{
+    int rank;
+    int size;
+
+    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+    MPI_Comm_size(MPI_COMM_WORLD, &size);
+
+    printf("Hai from %d\n", rank);
+
+    for (int j = 1; j <= i; j++)
+    {
+        if (j % size == rank)
+        {
+            printf("%d\n", j);
+        }
+        MPI_Barrier(MPI_COMM_WORLD);
+    }
+}

+ 16 - 0
tangle/src/omp.c

@@ -0,0 +1,16 @@
+#include "tangle.h"
+#include <stdio.h>
+#include <omp.h>
+
+void count_to(int i)
+{
+#pragma omp parallel num_threads(8)
+    {
+        int i = omp_get_thread_num();
+
+        for (int j = 1; j <= i; j++)
+        {
+            printf("MP %d: %d\n", i, j);
+        }
+    }
+}