Преглед изворни кода

- add matrix data registration
- add matrix example
- rename native_fortran_example and codelets files

Olivier Aumage пре 9 година
родитељ
комит
76a024d7e0

+ 19 - 10
examples/Makefile.am

@@ -229,7 +229,8 @@ if STARPU_HAVE_FC
 if !STARPU_SANITIZE
 STARPU_EXAMPLES +=				\
 	fortran90/f90_example			\
-	native_fortran/native_fortran_example
+	native_fortran/nf_vector	\
+	native_fortran/nf_matrix
 endif
 endif
 endif
@@ -349,10 +350,15 @@ fortran90_f90_example_SOURCES =	\
 	fortran90/marshalling.c		\
 	fortran90/f90_example.f90
 
-native_fortran_native_fortran_example_SOURCES =	\
-	native_fortran/codelets.f90		\
+native_fortran_nf_vector_SOURCES =	\
+	native_fortran/nf_codelets.f90		\
 	$(top_srcdir)/include/fstarpu_mod.f90	\
-	native_fortran/native_fortran_example.f90
+	native_fortran/nf_vector.f90
+
+native_fortran_nf_matrix_SOURCES =	\
+	native_fortran/nf_codelets.f90		\
+	$(top_srcdir)/include/fstarpu_mod.f90	\
+	native_fortran/nf_matrix.f90
 endif
 
 #######################
@@ -983,14 +989,17 @@ f90_example.o: $(top_srcdir)/examples/fortran90/f90_example.f90 $(top_srcdir)/ex
 # - the overriding rule fully disables the corresponing default rule, thus
 #   the default rule body must be copied entirely
 fstarpu_mod.mod: fstarpu_mod.o
-codelets.mod: codelets.o
+nf_codelets.mod: nf_codelets.o
 
 fstarpu_mod.o: $(top_srcdir)/include/fstarpu_mod.f90
-	$(AM_V_FC)$(FC) $(native_fortran_native_fortran_example_FCFLAGS) $(FCFLAGS) -c -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
+
+nf_codelets.o: $(top_srcdir)/examples/native_fortran/nf_codelets.f90 fstarpu_mod.mod
+	$(AM_V_FC)$(FC) $(native_fortran_nf_vector_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_codelets.f90' || echo '$(srcdir)/'`native_fortran/nf_codelets.f90
 
-codelets.o: $(top_srcdir)/examples/native_fortran/codelets.f90 fstarpu_mod.mod
-	$(AM_V_FC)$(FC) $(native_fortran_native_fortran_example_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/codelets.f90' || echo '$(srcdir)/'`native_fortran/codelets.f90
+nf_vector.o: $(top_srcdir)/examples/native_fortran/nf_vector.f90 nf_codelets.mod fstarpu_mod.mod
+	$(AM_V_FC)$(FC) $(native_fortran_nf_vector_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_vector.f90' || echo '$(srcdir)/'`native_fortran/nf_vector.f90
 
-native_fortran_example.o: $(top_srcdir)/examples/native_fortran/native_fortran_example.f90 codelets.mod fstarpu_mod.mod
-	$(AM_V_FC)$(FC) $(native_fortran_native_fortran_example_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/native_fortran_example.f90' || echo '$(srcdir)/'`native_fortran/native_fortran_example.f90
+matrix.o: $(top_srcdir)/examples/native_fortran/nf_matrix.f90 nf_codelets.mod fstarpu_mod.mod
+	$(AM_V_FC)$(FC) $(native_fortran_matrix_FCFLAGS) $(FCFLAGS) -c -o $@ `test -f 'native_fortran/nf_matrix.f90' || echo '$(srcdir)/'`native_fortran/nf_matrix.f90
 endif

+ 5 - 5
examples/native_fortran/Makefile

@@ -13,12 +13,12 @@
 #
 # See the GNU Lesser General Public License in COPYING.LGPL for more details.
 
-PROG = native_fortran_example
+PROG = nf_vector
 
 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 = native_fortran_example.f90		\
-	codelets.f90
+SRCSF = nf_vector.f90		\
+	nf_codelets.f90
 
 FC = gfortran
 CC = gcc
@@ -47,5 +47,5 @@ fstarpu_mod.o: $(FSTARPU_MOD)
 clean:
 	rm -fv *.o *.mod $(PROG)
 
-native_fortran_example.o: native_fortran_example.f90 codelets.o fstarpu_mod.o
-codelets.o: fstarpu_mod.o
+nf_vector.o: nf_vector.f90 nf_codelets.o fstarpu_mod.o
+nf_codelets.o: fstarpu_mod.o

+ 60 - 5
examples/native_fortran/codelets.f90

@@ -13,14 +13,14 @@
 !
 ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
 
-module codelets
+module nf_codelets
 contains
-        ! 'cl1' codelet routine
+        ! 'cl_vec' codelet routine
         !
         ! Note: codelet routines must:
         ! . be declared recursive (~ 'reentrant routine')
         ! . be declared with the 'bind(C)' attribute for proper C interfacing
-recursive subroutine cl_cpu_func1 (buffers, cl_args) bind(C)
+recursive subroutine cl_cpu_func_vec (buffers, cl_args) bind(C)
         use iso_c_binding       ! C interfacing module
         use fstarpu_mod         ! StarPU interfacing module
         implicit none
@@ -56,5 +56,60 @@ recursive subroutine cl_cpu_func1 (buffers, cl_args) bind(C)
         end do
         write(*,*) "task <--"
 
-end subroutine cl_cpu_func1
-end module codelets
+end subroutine cl_cpu_func_vec
+
+        ! 'cl_mat' codelet routine
+recursive subroutine cl_cpu_func_mat (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
+        real(8), dimension(:,:), pointer :: ma
+        integer, dimension(:,:), pointer :: mb
+        integer :: ldy_ma,ny_ma,nx_ma
+        integer :: ldy_mb,ny_mb,nx_mb
+        integer :: i,j
+
+        write(*,*) "task -->"
+        ldy_ma = fstarpu_matrix_get_ld(buffers, 0)
+        ny_ma = fstarpu_matrix_get_ny(buffers, 0)
+        nx_ma = fstarpu_matrix_get_nx(buffers, 0)
+        write(*,*) "ldy_ma"
+        write(*,*) ldy_ma
+        write(*,*) "ny_ma"
+        write(*,*) ny_ma
+        write(*,*) "nx_ma"
+        write(*,*) nx_ma
+
+        ldy_mb = fstarpu_matrix_get_ld(buffers, 1)
+        ny_mb = fstarpu_matrix_get_ny(buffers, 1)
+        nx_mb = fstarpu_matrix_get_nx(buffers, 1)
+        write(*,*) "ldy_mb"
+        write(*,*) ldy_mb
+        write(*,*) "ny_mb"
+        write(*,*) ny_mb
+        write(*,*) "nx_mb"
+        write(*,*) nx_mb
+
+        call c_f_pointer(fstarpu_matrix_get_ptr(buffers, 0), ma, shape=[ny_ma,nx_ma])
+        write(*,*) "ma"
+        do i=1,ny_ma
+        do j=1,nx_ma
+                write(*,*) i,j,ma(i,j)
+        end do
+        write(*,*) '-'
+        end do
+
+        call c_f_pointer(fstarpu_matrix_get_ptr(buffers, 1), mb, shape=[ny_mb,nx_mb])
+        write(*,*) "mb"
+        do i=1,ny_mb
+        do j=1,nx_mb
+                write(*,*) i,j,mb(i,j)
+        end do
+        write(*,*) '-'
+        end do
+        write(*,*) "task <--"
+
+end subroutine cl_cpu_func_mat
+end module nf_codelets

+ 82 - 0
examples/native_fortran/nf_matrix.f90

@@ -0,0 +1,82 @@
+program nf_matrix
+        use iso_c_binding       ! C interfacing module
+        use fstarpu_mod         ! StarPU interfacing module
+        use codelets
+        implicit none
+
+        real(8), dimension(:,:), allocatable, target :: ma
+        integer, dimension(:,:), allocatable, target :: mb
+        integer :: i,j
+
+        type(c_ptr) :: cl_mat      ! a pointer for the codelet structure
+        type(c_ptr) :: dh_va    ! a pointer for the 'ma' vector data handle
+        type(c_ptr) :: dh_vb    ! a pointer for the 'mb' vector data handle
+
+        allocate(ma(5,6))
+        do i=1,5
+        do j=1,6
+        ma(i,j) = (i*10)+j
+        end do
+        end do
+
+        allocate(mb(7,8))
+        do i=1,7
+        do j=1,8
+        mb(i,j) = (i*10)+j
+        end do
+        end do
+
+        ! initialize StarPU with default settings
+        call fstarpu_init()
+
+        ! allocate an empty codelet structure
+        cl_mat = fstarpu_codelet_allocate()
+
+        ! add a CPU implementation function to the codelet
+        call fstarpu_codelet_add_cpu_func(cl_mat, C_FUNLOC(cl_cpu_func_mat))
+
+        ! add a Read-only mode data buffer to the codelet
+        call fstarpu_codelet_add_buffer(cl_mat, FSTARPU_R)
+
+        ! add a Read-Write mode data buffer to the codelet
+        call fstarpu_codelet_add_buffer(cl_mat, FSTARPU_RW)
+
+        ! register 'ma', a vector of real(8) elements
+        dh_va = fstarpu_matrix_data_register(c_loc(ma), 5, 5, 6, c_sizeof(ma(1,1)), 0)
+
+        ! register 'mb', a vector of integer elements
+        dh_vb = fstarpu_matrix_data_register(c_loc(mb), 7, 7, 8, c_sizeof(mb(1,1)), 0)
+
+        ! insert a task with codelet cl_mat, and vectors 'ma' and 'mb'
+        !
+        ! Note: The array argument must follow the layout:
+        !   (/
+        !     <codelet_ptr>,
+        !     [<argument_type> [<argument_value(s)],]
+        !     . . .
+        !     C_NULL_PTR
+        !   )/
+        !
+        ! Note: The argument type for data handles is FSTARPU_DATA, regardless
+        ! of the buffer access mode (specified in the codelet)
+        call fstarpu_insert_task((/ cl_mat, FSTARPU_DATA, dh_va, FSTARPU_DATA, dh_vb, C_NULL_PTR /))
+
+        ! wait for task completion
+        call fstarpu_task_wait_for_all()
+
+        ! unregister 'ma'
+        call fstarpu_data_unregister(dh_va)
+
+        ! unregister 'mb'
+        call fstarpu_data_unregister(dh_vb)
+
+        ! free codelet structure
+        call fstarpu_codelet_free(cl_mat)
+
+        ! shut StarPU down
+        call fstarpu_shutdown()
+
+        deallocate(mb)
+        deallocate(ma)
+
+end program nf_matrix

+ 11 - 11
examples/native_fortran/native_fortran_example.f90

@@ -13,17 +13,17 @@
 !
 ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
 
-program native_fortran_example
+program nf_vector
         use iso_c_binding       ! C interfacing module
         use fstarpu_mod         ! StarPU interfacing module
-        use codelets
+        use nf_codelets
         implicit none
 
         real(8), dimension(:), allocatable, target :: va
         integer, dimension(:), allocatable, target :: vb
         integer :: i
 
-        type(c_ptr) :: cl1      ! a pointer for the codelet structure
+        type(c_ptr) :: cl_vec      ! a pointer for the codelet structure
         type(c_ptr) :: dh_va    ! a pointer for the 'va' vector data handle
         type(c_ptr) :: dh_vb    ! a pointer for the 'vb' vector data handle
 
@@ -37,16 +37,16 @@ program native_fortran_example
         call fstarpu_init()
 
         ! allocate an empty codelet structure
-        cl1 = fstarpu_codelet_allocate()
+        cl_vec = fstarpu_codelet_allocate()
 
         ! add a CPU implementation function to the codelet
-        call fstarpu_codelet_add_cpu_func(cl1, C_FUNLOC(cl_cpu_func1))
+        call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
 
         ! add a Read-only mode data buffer to the codelet
-        call fstarpu_codelet_add_buffer(cl1, FSTARPU_R)
+        call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
 
         ! add a Read-Write mode data buffer to the codelet
-        call fstarpu_codelet_add_buffer(cl1, FSTARPU_RW)
+        call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW)
 
         ! register 'va', a vector of real(8) elements
         dh_va = fstarpu_vector_data_register(c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))), 0)
@@ -54,7 +54,7 @@ program native_fortran_example
         ! register 'vb', a vector of integer elements
         dh_vb = fstarpu_vector_data_register(c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))), 0)
 
-        ! insert a task with codelet cl1, and vectors 'va' and 'vb'
+        ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
         !
         ! Note: The array argument must follow the layout:
         !   (/
@@ -66,7 +66,7 @@ program native_fortran_example
         !
         ! Note: The argument type for data handles is FSTARPU_DATA, regardless
         ! of the buffer access mode (specified in the codelet)
-        call fstarpu_insert_task((/ cl1, FSTARPU_DATA, dh_va, FSTARPU_DATA, dh_vb, C_NULL_PTR /))
+        call fstarpu_insert_task((/ cl_vec, FSTARPU_DATA, dh_va, FSTARPU_DATA, dh_vb, C_NULL_PTR /))
 
         ! wait for task completion
         call fstarpu_task_wait_for_all()
@@ -78,7 +78,7 @@ program native_fortran_example
         call fstarpu_data_unregister(dh_vb)
 
         ! free codelet structure
-        call fstarpu_codelet_free(cl1)
+        call fstarpu_codelet_free(cl_vec)
 
         ! shut StarPU down
         call fstarpu_shutdown()
@@ -86,5 +86,5 @@ program native_fortran_example
         deallocate(vb)
         deallocate(va)
 
-end program native_fortran_example
+end program nf_vector
 

+ 39 - 0
include/fstarpu_mod.f90

@@ -85,6 +85,45 @@ module fstarpu_mod
                         integer(c_int), value, intent(in) :: i
                 end function fstarpu_vector_get_nx
 
+                function fstarpu_matrix_data_register(matrix, ldy, ny, nx, elt_size, ram) bind(C)
+                        use iso_c_binding
+                        type(c_ptr) :: fstarpu_matrix_data_register
+                        type(c_ptr), value, intent(in) :: matrix
+                        integer(c_int), value, intent(in) :: ldy
+                        integer(c_int), value, intent(in) :: ny
+                        integer(c_int), value, intent(in) :: nx
+                        integer(c_size_t), value, intent(in) :: elt_size
+                        integer(c_int), value, intent(in) :: ram
+                end function fstarpu_matrix_data_register
+
+                function fstarpu_matrix_get_ptr(buffers, i) bind(C)
+                        use iso_c_binding
+                        type(c_ptr) :: fstarpu_matrix_get_ptr
+                        type(c_ptr), value, intent(in) :: buffers
+                        integer(c_int), value, intent(in) :: i
+                end function fstarpu_matrix_get_ptr
+
+                function fstarpu_matrix_get_ld(buffers, i) bind(C)
+                        use iso_c_binding
+                        integer(c_int) :: fstarpu_matrix_get_ld
+                        type(c_ptr), value, intent(in) :: buffers
+                        integer(c_int), value, intent(in) :: i
+                end function fstarpu_matrix_get_ld
+
+                function fstarpu_matrix_get_ny(buffers, i) bind(C)
+                        use iso_c_binding
+                        integer(c_int) :: fstarpu_matrix_get_ny
+                        type(c_ptr), value, intent(in) :: buffers
+                        integer(c_int), value, intent(in) :: i
+                end function fstarpu_matrix_get_ny
+
+                function fstarpu_matrix_get_nx(buffers, i) bind(C)
+                        use iso_c_binding
+                        integer(c_int) :: fstarpu_matrix_get_nx
+                        type(c_ptr), value, intent(in) :: buffers
+                        integer(c_int), value, intent(in) :: i
+                end function fstarpu_matrix_get_nx
+
                 subroutine fstarpu_data_unregister (dh) bind(C,name="starpu_data_unregister")
                         use iso_c_binding, only: c_ptr
                         type(c_ptr), value, intent(in) :: dh

+ 37 - 0
src/util/fstarpu.c

@@ -140,6 +140,43 @@ int fstarpu_vector_get_nx(void *buffers[], int i)
 	return STARPU_VECTOR_GET_NX(buffers[i]);
 }
 
+starpu_data_handle_t fstarpu_matrix_data_register(void *matrix, int ldy, int ny, int nx, size_t elt_size, int ram)
+{
+	starpu_data_handle_t handle;
+	/*
+	 * Fortran arrays are transposed with respect to C arrays. For convenience, we exchange
+	 * the parameters as follows:
+	 * C ldx = Fortran ldy
+	 * C nx  = Fortran ny
+	 * C ny  = Fortran nx
+	 */
+	starpu_matrix_data_register(&handle, ram, (uintptr_t)matrix, ldy, ny, nx, elt_size);
+	return handle;
+}
+
+void * fstarpu_matrix_get_ptr(void *buffers[], int i)
+{
+	return (void *)STARPU_MATRIX_GET_PTR(buffers[i]);
+}
+
+int fstarpu_matrix_get_ld(void *buffers[], int i)
+{
+	/* Fortran ldy is C ldx */
+	return STARPU_MATRIX_GET_LD(buffers[i]);
+}
+
+int fstarpu_matrix_get_ny(void *buffers[], int i)
+{
+	/* Fortran ny is C nx */
+	return STARPU_MATRIX_GET_NX(buffers[i]);
+}
+
+int fstarpu_matrix_get_nx(void *buffers[], int i)
+{
+	/* Fortran nx is C ny */
+	return STARPU_MATRIX_GET_NY(buffers[i]);
+}
+
 void fstarpu_insert_task(void ***_arglist)
 {
 	void **arglist = *_arglist;