Skip to content
Snippets Groups Projects
Commit b00bfff5 authored by Nils-Arne Dreier's avatar Nils-Arne Dreier
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
NVHPC_ROOT=/sw/hpcx/hpcx-v2.21-gcc-doca_ofed-redhat8-cuda12-x86_64/hpcx_nvhpc-24.11_mt
FC=${NVHPC_ROOT}/bin/mpifort
CC=${NVHPC_ROOT}/bin/mpicc
FFLAGS=-g -O0 -cuda -acc=gpu,verystrict
CFLAGS=-g -O0 -fPIC -cuda
all: main plugin.so
main: main.F90
${FC} ${FFLAGS} $< -o $@
plugin.so: plugin.c
${CC} ${CFLAGS} $< -shared -o $@
The segmentation fault only occurs on GPU nodes.
E.g.:
```
salloc -p gpu --exclusive
---
make
./main
```
main.F90 0 → 100644
PROGRAM main
USE ISO_C_BINDING
INTERFACE
FUNCTION dlopen(filename,mode) BIND(c,name="dlopen")
! void *dlopen(const char *filename, int mode);
USE ISO_C_BINDING
TYPE(c_ptr) :: dlopen
TYPE(c_ptr), VALUE, INTENT(in) :: filename
INTEGER(c_int), VALUE :: mode
END FUNCTION dlopen
FUNCTION dlclose(handle) BIND(c,name="dlclose")
! int dlclose(void *handle);
USE ISO_C_BINDING
INTEGER(c_int) :: dlclose
TYPE(c_ptr), VALUE :: handle
END FUNCTION dlclose
FUNCTION DLError() RESULT(error) BIND(C,NAME="dlerror")
! char *dlerror(void);
USE ISO_C_BINDING
TYPE(C_PTR) :: error
END FUNCTION DLError
END INTERFACE
TYPE(C_PTR) :: dlhandle
INTEGER(C_INT) :: dlclose_result
INTEGER(C_INT), PARAMETER :: RTLD_NOW = 2
CHARACTER(LEN=:), ALLOCATABLE, TARGET :: so_name
INTEGER :: ierr
so_name = "./plugin.so" // C_NULL_CHAR
WRITE (0,*) "Moin!"
dlhandle = dlopen(C_LOC(so_name), RTLD_NOW)
IF(.NOT. C_ASSOCIATED(dlhandle)) THEN
WRITE (0,*) convert_c_string_cptr(dlerror())
STOP 66
ENDIF
dlclose_result = dlclose(dlhandle)
IF(dlclose_result /= 0) THEN
WRITE (0,*) convert_c_string_cptr(dlerror())
STOP 67
ENDIF
CONTAINS
!> Convert c-style character array into Fortran string.
!
FUNCTION convert_c_string_cptr( cptr ) RESULT (string)
USE, intrinsic :: iso_c_binding, only: c_ptr, c_char, &
c_f_pointer,c_size_t
TYPE(c_ptr), intent(in) :: cptr
CHARACTER(len=:), allocatable :: string
CHARACTER(kind=c_char), dimension(:), pointer :: chars
INTEGER(kind=c_size_t) :: strlen
INTERFACE
FUNCTION c_strlen(str_ptr) BIND ( C, name = "strlen" ) RESULT(len)
USE, INTRINSIC :: iso_c_binding
TYPE(c_ptr), VALUE :: str_ptr
INTEGER(kind=c_size_t) :: len
END FUNCTION c_strlen
END INTERFACE
strlen = c_strlen(cptr)
CALL c_f_pointer(cptr, chars, [ strlen ])
string = convert_c_string_arr(chars)
END FUNCTION convert_c_string_cptr
FUNCTION convert_c_string_arr( arr ) RESULT (string)
CHARACTER(kind=C_CHAR), INTENT(IN) :: arr(:)
CHARACTER(len=:), ALLOCATABLE :: string
INTEGER :: i, strlen
DO strlen=1,SIZE(arr)
IF (arr(strlen) .EQ. c_null_char) EXIT
END DO
ALLOCATE(CHARACTER(len=strlen-1) :: string)
DO i=1,strlen-1
string(i:i) = arr(i)
END DO
END FUNCTION convert_c_string_arr
END PROGRAM main
#include <stdio.h>
void foo(){
printf("FOO");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment