- 
                Notifications
    You must be signed in to change notification settings 
- Fork 36
Add OpenMP kernels #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
        
      
            mcbarton
  wants to merge
  7
  commits into
  compiler-research:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
mcbarton:Add-OpenMP-kernel
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Add OpenMP kernels #389
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      d1c82b5
              
                Add c++17,c++20 and c++23 kernels all with openmp
              
              
                mcbarton 9d4a18c
              
                Update cmake to configure added c++ + openmp kernels + add llvm-openm…
              
              
                mcbarton baad01b
              
                Add example notebooks for c++ + openmp kernels
              
              
                mcbarton a70d4a5
              
                Add c + openmp kernels
              
              
                mcbarton 2545f10
              
                Configure c + openmp kernels cmake
              
              
                mcbarton e3a9484
              
                Run existing tests for OpenMP kernels
              
              
                mcbarton 47157b6
              
                Add Simple OpenMP kernel test
              
              
                mcbarton File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "id": "73cbab37-71dd-477d-981b-f2ec28c01bd6", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "#include <stdio.h>\n", | ||
| "#include <omp.h>\n", | ||
| "#include <clang/Interpreter/CppInterOp.h>" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "id": "ef1cd58a-672c-4a6f-843a-6c88fc4911f3", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "Cpp::LoadLibrary(\"libomp\");\n", | ||
| " " | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "id": "c2b754ad-9553-4a42-b990-f990a9a269ed", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "int main() {\n", | ||
| " int max_threads = omp_get_max_threads();\n", | ||
| "\n", | ||
| " printf(\"max threads: %d\\n\", max_threads);\n", | ||
| " omp_set_num_threads(max_threads);\n", | ||
| "\n", | ||
| "#pragma omp parallel\n", | ||
| " {\n", | ||
| " int id = omp_get_thread_num();\n", | ||
| " printf(\"Hello World from thread = %d with %d threads\\n\", id, omp_get_num_threads());\n", | ||
| " }\n", | ||
| "\n", | ||
| " printf(\"all done, with hopefully %d threads\\n\", max_threads);\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 4, | ||
| "id": "a37a13d4-fc82-496e-8f42-9e718a8c2aa0", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "max threads: 8\n", | ||
| "Hello World from thread = 0 with 8 threads\n", | ||
| "Hello World from thread = 3 with 8 threads\n", | ||
| "Hello World from thread = 4 with 8 threads\n", | ||
| "Hello World from thread = 2 with 8 threads\n", | ||
| "Hello World from thread = 7 with 8 threads\n", | ||
| "Hello World from thread = 1 with 8 threads\n", | ||
| "Hello World from thread = 6 with 8 threads\n", | ||
| "Hello World from thread = 5 with 8 threads\n", | ||
| "all done, with hopefully 8 threads\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "main();" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "C++17 (xcpp+OpenMP)", | ||
| "language": "cpp", | ||
| "name": "xcpp17-omp" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": "text/x-c++src", | ||
| "file_extension": ".cpp", | ||
| "mimetype": "text/x-c++src", | ||
| "name": "C++", | ||
| "version": "17" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "id": "156447d2-9279-45a0-890b-4e519d2c796b", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "#include <stdlib.h>\n", | ||
| "#include <stdio.h>\n", | ||
| "#include <omp.h>\n", | ||
| "#include <clang/Interpreter/CppInterOp.h>" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "id": "c96fdeb0-817d-48c0-af8e-20a52947d60b", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "#ifndef N\n", | ||
| "#define N 5\n", | ||
| "#endif\n", | ||
| "#ifndef FS\n", | ||
| "#define FS 38\n", | ||
| "#endif" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "id": "8da842e1-db02-49e0-929d-4e67cbc08172", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "Cpp::LoadLibrary(\"libomp\");" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 4, | ||
| "id": "22f97c49-78d1-496e-ac7c-978aed95331a", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "struct node {\n", | ||
| " int data;\n", | ||
| " int fibdata;\n", | ||
| " struct node *next;\n", | ||
| "};" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 5, | ||
| "id": "b16b1e8a-8831-4b8d-9d57-09deeaaa88ee", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "struct node *init_list(struct node *p);\n", | ||
| "void processwork(struct node *p);\n", | ||
| "int fib(int n);" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 6, | ||
| "id": "0ef8af6c-1d6f-4c68-84bc-3dd1d8092b06", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "int fib(int n) {\n", | ||
| " int x, y;\n", | ||
| " if (n < 2) {\n", | ||
| " return (n);\n", | ||
| " } else {\n", | ||
| " x = fib(n - 1);\n", | ||
| " y = fib(n - 2);\n", | ||
| " return (x + y);\n", | ||
| " }\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 7, | ||
| "id": "1fa0307d-fdc9-4503-95cb-1c6448791354", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "void processwork(struct node *p) {\n", | ||
| " int n, temp;\n", | ||
| " n = p->data;\n", | ||
| " temp = fib(n);\n", | ||
| "\n", | ||
| " p->fibdata = temp;\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 8, | ||
| "id": "03acb599-9329-49ff-8aff-c0902adb6c3c", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "struct node *init_list(struct node *p) {\n", | ||
| " int i;\n", | ||
| " struct node *head = NULL;\n", | ||
| " struct node *temp = NULL;\n", | ||
| "\n", | ||
| " head = (struct node*) malloc(sizeof(struct node));\n", | ||
| " p = head;\n", | ||
| " p->data = FS;\n", | ||
| " p->fibdata = 0;\n", | ||
| " for (i = 0; i < N; i++) {\n", | ||
| " temp = (struct node*) malloc(sizeof(struct node));\n", | ||
| " p->next = temp;\n", | ||
| " p = temp;\n", | ||
| " p->data = FS + i + 1;\n", | ||
| " p->fibdata = i + 1;\n", | ||
| " }\n", | ||
| "\n", | ||
| " p->next = NULL;\n", | ||
| " return head;\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 9, | ||
| "id": "f2dfb41b-e55f-43c0-b7f6-546a1697acb1", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "int main() {\n", | ||
| " double start, end;\n", | ||
| " struct node *p = NULL;\n", | ||
| " struct node *temp = NULL;\n", | ||
| " struct node *head = NULL;\n", | ||
| "\n", | ||
| " printf(\"Process linked list\\n\");\n", | ||
| " printf(\" Each linked list node will be processed by function 'processwork()'\\n\");\n", | ||
| " printf(\" Each ll node will compute %d fibonacci numbers beginning with %d\\n\", N, FS);\n", | ||
| "\n", | ||
| " omp_set_num_threads(omp_get_max_threads());\n", | ||
| "\n", | ||
| " p = init_list(p);\n", | ||
| " head = p;\n", | ||
| "\n", | ||
| " start = omp_get_wtime();\n", | ||
| "\n", | ||
| "#pragma omp parallel\n", | ||
| " {\n", | ||
| "#pragma omp master\n", | ||
| " printf(\"Threads: %d\\n\", omp_get_num_threads());\n", | ||
| "\n", | ||
| "#pragma omp single\n", | ||
| " {\n", | ||
| " p = head;\n", | ||
| " while (p) {\n", | ||
| "#pragma omp task firstprivate(p) // first private is required\n", | ||
| " {\n", | ||
| " processwork(p);\n", | ||
| " }\n", | ||
| " p = p->next;\n", | ||
| " }\n", | ||
| " }\n", | ||
| " }\n", | ||
| "\n", | ||
| " end = omp_get_wtime();\n", | ||
| " p = head;\n", | ||
| " while (p != NULL) {\n", | ||
| " printf(\"%d : %d\\n\", p->data, p->fibdata);\n", | ||
| " temp = p->next;\n", | ||
| " free(p);\n", | ||
| " p = temp;\n", | ||
| " }\n", | ||
| "\n", | ||
| " free(p);\n", | ||
| " printf(\"Compute Time: %f seconds\\n\", end - start);\n", | ||
| "\n", | ||
| " return 0;\n", | ||
| "}" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 10, | ||
| "id": "353e5dfd-fcae-43e6-97e3-ec98070811a1", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Process linked list\n", | ||
| " Each linked list node will be processed by function 'processwork()'\n", | ||
| " Each ll node will compute 5 fibonacci numbers beginning with 38\n", | ||
| "Threads: 8\n", | ||
| "38 : 39088169\n", | ||
| "39 : 63245986\n", | ||
| "40 : 102334155\n", | ||
| "41 : 165580141\n", | ||
| "42 : 267914296\n", | ||
| "43 : 433494437\n", | ||
| "Compute Time: 2.617225 seconds\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "main();" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "C++17 (xcpp+OpenMP)", | ||
| "language": "cpp", | ||
| "name": "xcpp17-omp" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": "text/x-c++src", | ||
| "file_extension": ".cpp", | ||
| "mimetype": "text/x-c++src", | ||
| "name": "C++", | ||
| "version": "17" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the path to the openmp library path from cmake is how I managed to avoid having the user manual set ld_library_path. The kernels are then configured using this variable. @vgvassilev @Vipul-Cariappa Hopefully this an acceptable alternative. LD_LIBRARY_PATH is still mentioned in the kernelspec, but it no longer comes from the user setting variables. Its needed otherwise CppInterOp doesn't know where the openmp library is it needs to load.
I couldn't get xeus-cpp to parse the arguments of the kernelspec and it finds
-fopenmp, to automatically include CppInterOp.h and callCpp::LoadLibrary(...)on the openmp library. Can I open this as an issue instead of fixing it as part of this PR?