Browse Source

- add example of Fortran code using dynamic buffers

Olivier Aumage 9 years ago
parent
commit
f6915b2b0c

+ 16 - 3
examples/Makefile.am

@@ -241,9 +241,10 @@ if STARPU_HAVE_FC
 if !STARPU_SANITIZE
 STARPU_EXAMPLES +=				\
 	fortran90/f90_example			\
-	native_fortran/nf_vector	\
-	native_fortran/nf_matrix	\
-	native_fortran/nf_example
+	native_fortran/nf_vector		\
+	native_fortran/nf_matrix		\
+	native_fortran/nf_example		\
+	native_fortran/nf_dynbuf
 endif
 endif
 endif
@@ -386,6 +387,11 @@ native_fortran_nf_example_SOURCES =	\
 	native_fortran/nf_compute.f90		\
 	$(top_srcdir)/include/fstarpu_mod.f90	\
 	native_fortran/nf_example.f90
+
+native_fortran_nf_dynbuf_SOURCES =	\
+	native_fortran/nf_dynbuf_cl.f90		\
+	$(top_srcdir)/include/fstarpu_mod.f90	\
+	native_fortran/nf_dynbuf.f90
 endif
 
 #######################
@@ -1019,6 +1025,7 @@ nf_types.mod: nf_types.o
 nf_compute.mod: nf_compute.o
 fstarpu_mod.mod: fstarpu_mod.o
 nf_codelets.mod: nf_codelets.o
+nf_dynbuf_cl.mod: nf_dynbuf_cl.o
 
 fstarpu_mod.o: $(top_srcdir)/include/fstarpu_mod.f90
 	$(AM_V_FC)$(FC) $(native_fortran_nf_vector_FCFLAGS) $(FCFLAGS) -c -o $@ '$(top_srcdir)/'include/fstarpu_mod.f90
@@ -1038,4 +1045,10 @@ nf_compute.o: $(top_srcdir)/examples/native_fortran/nf_compute.f90 nf_types.mod
 nf_example.o: $(top_srcdir)/examples/native_fortran/nf_example.f90 nf_types.mod nf_compute.mod fstarpu_mod.mod
 	$(AM_V_FC)$(FC) $(native_fortran_nf_example_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_example.f90' || echo '$(srcdir)/'`native_fortran/nf_example.f90
 
+nf_dynbuf_cl.o: $(top_srcdir)/examples/native_fortran/nf_dynbuf_cl.f90 nf_types.mod fstarpu_mod.mod
+	$(AM_V_FC)$(FC) $(native_fortran_nf_dynbuf_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_dynbuf_cl.f90' || echo '$(srcdir)/'`native_fortran/nf_dynbuf_cl.f90
+
+nf_dynbuf.o: $(top_srcdir)/examples/native_fortran/nf_dynbuf.f90 nf_types.mod nf_dynbuf_cl.mod fstarpu_mod.mod
+	$(AM_V_FC)$(FC) $(native_fortran_nf_dynbuf_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_dynbuf.f90' || echo '$(srcdir)/'`native_fortran/nf_dynbuf.f90
+
 endif

+ 48 - 0
examples/native_fortran/Makefile.nf_dynbuf

@@ -0,0 +1,48 @@
+# StarPU --- Runtime system for heterogeneous multicore architectures.
+#
+# Copyright (C) 2015  ONERA
+# Copyright (C) 2015-2016  Inria
+#
+# StarPU is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation; either version 2.1 of the License, or (at
+# your option) any later version.
+#
+# StarPU is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#
+# See the GNU Lesser General Public License in COPYING.LGPL for more details.
+
+PROG = nf_dynbuf
+
+FSTARPU_MOD = $(shell pkg-config --cflags-only-I starpu-1.3|sed -e 's/^\([^ ]*starpu\/1.3\).*$$/\1/;s/^.* //;s/^-I//')/fstarpu_mod.f90
+
+SRCSF = nf_dynbuf_cl.f90	\
+	nf_dynbuf.f90
+
+FC = gfortran
+
+FCFLAGS = -fdefault-real-8 -J. -g
+LDLIBS =  $(shell pkg-config --libs starpu-1.3)
+
+OBJS = fstarpu_mod.o $(SRCSF:%.f90=%.o)
+
+.phony: all clean
+all: $(PROG)
+
+$(PROG): $(OBJS)
+	$(FC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+fstarpu_mod.o: $(FSTARPU_MOD)
+	$(FC) $(FCFLAGS) -c -o $@ $<
+
+%.o: %.f90
+	$(FC) $(FCFLAGS) -c -o $@ $<
+
+clean:
+	rm -fv *.o *.mod $(PROG)
+
+# modfiles generation dependences
+nf_dynbuf_cl.o: nf_dynbuf_cl.f90 fstarpu_mod.o
+nf_dynbuf.o: nf_dynbuf.f90 nf_types.o fstarpu_mod.o

+ 62 - 0
examples/native_fortran/nf_dynbuf.f90

@@ -0,0 +1,62 @@
+program nf_dynbuf
+        use iso_c_binding       ! C interfacing module
+        use fstarpu_mod         ! StarPU interfacing module
+        use nf_dynbuf_cl
+        implicit none
+
+        type(c_ptr) :: cl_dynbuf_big   ! a pointer for the codelet structure
+        type(c_ptr) :: dh_var
+        type(c_ptr) :: descrs_var
+        integer(c_int),target :: nbuffers
+        integer(c_int) :: err   ! return status for fstarpu_init
+        integer(c_int) :: ncpu  ! number of cpus workers
+
+        integer(c_int),target :: var
+        integer(c_int) :: i
+
+        var = 42
+
+        ! initialize StarPU with default settings
+        err = fstarpu_init(C_NULL_PTR)
+        if (err == -19) then
+                stop 77
+        end if
+
+        ! stop there if no CPU worker available
+        ncpu = fstarpu_cpu_worker_get_count()
+        if (ncpu == 0) then
+                call fstarpu_shutdown()
+                stop 77
+        end if
+
+        ! allocate an empty codelet structure
+        cl_dynbuf_big = fstarpu_codelet_allocate()
+        call fstarpu_codelet_set_name(cl_dynbuf_big, C_CHAR_"dummy_big_kernel"//C_NULL_CHAR)
+        call fstarpu_codelet_add_cpu_func(cl_dynbuf_big, C_FUNLOC(cl_cpu_func_dynbuf_big))
+
+        write(*,*) "FSTARPU_NMAXBUFS",FSTARPU_NMAXBUFS
+        nbuffers = FSTARPU_NMAXBUFS+1
+        call fstarpu_codelet_set_nbuffers(cl_dynbuf_big, nbuffers)
+
+        call fstarpu_variable_data_register(dh_var, 0, c_loc(var), c_sizeof(var))
+
+        descrs_var = fstarpu_data_descr_array_alloc(nbuffers)
+        do i=0,nbuffers-1
+                call fstarpu_data_descr_array_set(descrs_var, i, dh_var, FSTARPU_RW)
+        end do
+        call fstarpu_insert_task((/ cl_dynbuf_big, &
+                FSTARPU_VALUE, c_loc(nbuffers), FSTARPU_SZ_C_INT, &
+                FSTARPU_DATA_MODE_ARRAY, descrs_var, c_loc(nbuffers), &
+                C_NULL_PTR /))
+        call fstarpu_task_wait_for_all()
+
+        call fstarpu_data_descr_array_free(descrs_var)
+        call fstarpu_data_unregister(dh_var)
+
+        ! free codelet structure
+        call fstarpu_codelet_free(cl_dynbuf_big)
+
+        ! shut StarPU down
+        call fstarpu_shutdown()
+
+end program nf_dynbuf

+ 23 - 0
examples/native_fortran/nf_dynbuf_cl.f90

@@ -0,0 +1,23 @@
+module nf_dynbuf_cl
+contains
+recursive subroutine cl_cpu_func_dynbuf_big (buffers, cl_args) bind(C)
+        use iso_c_binding       ! C interfacing module
+        use fstarpu_mod         ! StarPU interfacing module
+        implicit none
+
+        type(c_ptr), value, intent(in) :: buffers, cl_args ! cl_args is unused
+        integer(c_int),target :: nb_data
+        integer(c_int),pointer :: val
+        integer(c_int) :: i
+
+        call fstarpu_unpack_arg(cl_args,(/ c_loc(nb_data) /))
+        write(*,*) "number of data:", nb_data
+        do i=0,nb_data-1
+                call c_f_pointer(fstarpu_variable_get_ptr(buffers, i), val)
+                write(*,*) "i:", i, ", val:", val
+                if (val /= 42) then
+                        stop 1
+                end if
+        end do
+end subroutine cl_cpu_func_dynbuf_big
+end module nf_dynbuf_cl

+ 19 - 2
include/fstarpu_mod.f90

@@ -62,6 +62,10 @@ module fstarpu_mod
         type(c_ptr), bind(C) :: FSTARPU_SCC_WORKER
         type(c_ptr), bind(C) :: FSTARPU_ANY_WORKER
 
+        integer(c_int), bind(C) :: FSTARPU_NMAXBUFS
+
+        type(c_ptr), bind(C) :: FSTARPU_SZ_C_INT
+        type(c_ptr), bind(C) :: FSTARPU_SZ_C_INTPTR_T
         type(c_ptr), bind(C) :: FSTARPU_SZ_INT4
         type(c_ptr), bind(C) :: FSTARPU_SZ_INT8
 
@@ -1403,7 +1407,7 @@ module fstarpu_mod
                         type(c_ptr), value, intent(in) :: descrs
                         integer(c_int), value, intent(in) :: i
                         type(c_ptr), value, intent(in) :: handle
-                        integer(c_intptr_t), value, intent(in) :: mode
+                        type(c_ptr), value, intent(in) :: mode ! C func expects c_intptr_t
                 end subroutine fstarpu_data_descr_array_set
 
                 ! void fstarpu_data_descr_set(struct starpu_data_descr *descr, starpu_data_handle_t handle, intptr_t mode);
@@ -1411,7 +1415,7 @@ module fstarpu_mod
                         use iso_c_binding, only: c_ptr, c_intptr_t
                         type(c_ptr), value, intent(in) :: descr
                         type(c_ptr), value, intent(in) :: handle
-                        integer(c_intptr_t), value, intent(in) :: mode
+                        type(c_ptr), value, intent(in) :: mode ! C func expects c_intptr_t
                 end subroutine fstarpu_data_descr_set
 
 
@@ -1563,6 +1567,13 @@ module fstarpu_mod
                         ip_to_p = transfer(i,C_NULL_PTR)
                 end function ip_to_p
 
+                function p_to_ip(p) bind(C)
+                        use iso_c_binding, only: c_ptr,c_intptr_t
+                        integer(c_intptr_t) :: p_to_ip
+                        type(c_ptr), value, intent(in) :: p
+                        p_to_ip = transfer(p,0_c_intptr_t)
+                end function p_to_ip
+
                 function sz_to_p(sz) bind(C)
                         use iso_c_binding, only: c_ptr,c_size_t,c_intptr_t
                         type(c_ptr) :: sz_to_p
@@ -1575,6 +1586,8 @@ module fstarpu_mod
                         integer(c_int) :: fstarpu_init
                         type(c_ptr), value, intent(in) :: conf
 
+                        integer(c_int) :: FSTARPU_SZ_C_INT_dummy
+                        integer(c_intptr_t) :: FSTARPU_SZ_C_INTPTR_T_dummy
                         integer(4) :: FSTARPU_SZ_INT4_dummy
                         integer(8) :: FSTARPU_SZ_INT8_dummy
                         real(4) :: FSTARPU_SZ_REAL4_dummy
@@ -1644,7 +1657,11 @@ module fstarpu_mod
                         FSTARPU_SCC_WORKER   = fstarpu_get_constant(C_CHAR_"FSTARPU_SCC_WORKER"//C_NULL_CHAR)
                         FSTARPU_ANY_WORKER   = fstarpu_get_constant(C_CHAR_"FSTARPU_ANY_WORKER"//C_NULL_CHAR)
 
+                        FSTARPU_NMAXBUFS   = int(p_to_ip(fstarpu_get_constant(C_CHAR_"FSTARPU_NMAXBUFS"//C_NULL_CHAR)),c_int)
+
                         ! Initialize size constants as 'c_ptr'
+                        FSTARPU_SZ_C_INT        = sz_to_p(c_sizeof(FSTARPU_SZ_C_INT_dummy))
+                        FSTARPU_SZ_C_INTPTR_T   = sz_to_p(c_sizeof(FSTARPU_SZ_C_INTPTR_T_dummy))
                         FSTARPU_SZ_INT4         = sz_to_p(c_sizeof(FSTARPU_SZ_INT4_dummy))
                         FSTARPU_SZ_INT8         = sz_to_p(c_sizeof(FSTARPU_SZ_INT8_dummy))
                         FSTARPU_SZ_REAL4        = sz_to_p(c_sizeof(FSTARPU_SZ_REAL4_dummy))

+ 5 - 1
src/util/fstarpu.c

@@ -65,6 +65,8 @@ static const intptr_t fstarpu_mic_worker = STARPU_MIC_WORKER;
 static const intptr_t fstarpu_scc_worker = STARPU_SCC_WORKER;
 static const intptr_t fstarpu_any_worker = STARPU_ANY_WORKER;
 
+static const intptr_t fstarpu_nmaxbufs = STARPU_NMAXBUFS;
+
 intptr_t fstarpu_get_constant(char *s)
 {
 	if	(!strcmp(s, "FSTARPU_R"))	{ return fstarpu_r; }
@@ -109,7 +111,9 @@ intptr_t fstarpu_get_constant(char *s)
 	else if (!strcmp(s, "FSTARPU_SCC_WORKER"))	{ return fstarpu_scc_worker; }
 	else if (!strcmp(s, "FSTARPU_ANY_WORKER"))	{ return fstarpu_any_worker; }
 
-	else { _FSTARPU_ERROR("unknown pointer constant"); }
+	else if (!strcmp(s, "FSTARPU_NMAXBUFS"))	{ return fstarpu_nmaxbufs; }
+
+	else { _FSTARPU_ERROR("unknown constant"); }
 }
 
 struct starpu_conf *fstarpu_conf_allocate(void)