Re: libmvec simd math functions in fortran
On Thu, Nov 2, 2017 at 7:42 PM, Toon Moenewrote: > On 11/01/2017 05:26 PM, Jakub Jelinek wrote: > >> On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: >>> >>> is there a way to get vectorized math functions in fortran? >>> >>> in c code there is attribute simd declarations or openmp >>> declare simd pragma to tell the compiler which functions >>> have simd variant, but i see no such thing in fortran. >> >> >> !$omp declare simd should work fine in fortran (with -fopenmp >> or -fopenmp-simd). > > > Note that - if you don't want to change the Fortran code, this - almost two > years old - proposal would work: > > https://gcc.gnu.org/ml/gcc/2016-01/msg00025.html > > Obviously, I'll only be able to implement this once retirement comes around > (i.e., after 2023). I think a better user-interface would be gfortran ... -include mathvec which pulls in a pre-def for the vectorized math functions as available. Somewhere that mathvec "module" needs to reside, ideally it came from the local glibc install (but module files are GCC version dependent?). Not sure if gfortran supports -include for module files, OTOH a -include of fortran source should also work in all contexts(?). The implementation challenge is 1) actually being able to declare the relevant math functions appropriately (say, for -ffixed-form f77 style source) 2) make gfortran pick the FUNCTION_DECLs from those declarations instead of those generated from the mathbuiltins.def. Solving 2) would be nice to make this work "manually" anyway. I'm not quite sure we properly override / merge (for other attributes) with the builtins. Richard. > Kind regards, > > -- > Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 > Saturnushof 14, 3738 XG Maartensdijk, The Netherlands > At home: http://moene.org/~toon/; weather: http://moene.org/~hirlam/ > Progress of GNU Fortran: http://gcc.gnu.org/wiki/GFortran#news
Re: libmvec simd math functions in fortran
On 11/01/2017 05:26 PM, Jakub Jelinek wrote: On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: is there a way to get vectorized math functions in fortran? in c code there is attribute simd declarations or openmp declare simd pragma to tell the compiler which functions have simd variant, but i see no such thing in fortran. !$omp declare simd should work fine in fortran (with -fopenmp or -fopenmp-simd). Note that - if you don't want to change the Fortran code, this - almost two years old - proposal would work: https://gcc.gnu.org/ml/gcc/2016-01/msg00025.html Obviously, I'll only be able to implement this once retirement comes around (i.e., after 2023). Kind regards, -- Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 Saturnushof 14, 3738 XG Maartensdijk, The Netherlands At home: http://moene.org/~toon/; weather: http://moene.org/~hirlam/ Progress of GNU Fortran: http://gcc.gnu.org/wiki/GFortran#news
Re: libmvec simd math functions in fortran
On Thu, Nov 2, 2017 at 2:49 PM, Szabolcs Nagywrote: > On 01/11/17 16:47, Szabolcs Nagy wrote: >> On 01/11/17 16:26, Jakub Jelinek wrote: >>> On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: is there a way to get vectorized math functions in fortran? in c code there is attribute simd declarations or openmp declare simd pragma to tell the compiler which functions have simd variant, but i see no such thing in fortran. >>> >>> !$omp declare simd should work fine in fortran (with -fopenmp >>> or -fopenmp-simd). >>> >> >> 1) i don't want to change the fortran. >> >> 2) it does not work for me. >> >> i want this to call vector powf in libmvec: >> >> subroutine foo(a,b,c) >> real(4) a(8000),b(8000),c(8000) >> do j=1,8000 >> a(j)=b(j)**c(j) >> end do >> end >> >> where do i put >> >> !$omp declare simd (powf) >> >> ? > > to answer my question.. > > it seems fortran cannot express the type signature > of mathematical functions because arguments are > passed by reference. > > so there is no way to declare math interfaces > and then add omp declare simd to them to get > simd versions. > > (it's not clear to me how omp declare simd is > supposed to work in fortran, but it is not useful > for vectorizing loops with math functions.) > > so gfortran will need a different mechanism to > do the vectorization, e.g. an option like > -mveclibabi=glibc, but the list of available > vector functions need to be specified somewhere. The fortran frontend turns the b(j)**c(j) expression in your example into a "call" to __builtin_powf() (check with -fdump-tree-original), and similarly for other math intrinsics that have corresponding builtins. Ideally the middle-end optimizers should to be able to turn that into calls to the appropriate vector math library when -mveclibabi= is in effect. -- Janne Blomqvist
Re: libmvec simd math functions in fortran
On Thu, Nov 2, 2017 at 2:07 PM, Richard Bienerwrote: > On Thu, Nov 2, 2017 at 1:49 PM, Szabolcs Nagy wrote: >> On 01/11/17 16:47, Szabolcs Nagy wrote: >>> On 01/11/17 16:26, Jakub Jelinek wrote: On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: > is there a way to get vectorized math functions in fortran? > > in c code there is attribute simd declarations or openmp > declare simd pragma to tell the compiler which functions > have simd variant, but i see no such thing in fortran. !$omp declare simd should work fine in fortran (with -fopenmp or -fopenmp-simd). >>> >>> 1) i don't want to change the fortran. >>> >>> 2) it does not work for me. >>> >>> i want this to call vector powf in libmvec: >>> >>> subroutine foo(a,b,c) >>> real(4) a(8000),b(8000),c(8000) >>> do j=1,8000 >>> a(j)=b(j)**c(j) >>> end do >>> end >>> >>> where do i put >>> >>> !$omp declare simd (powf) >>> >>> ? >> >> to answer my question.. >> >> it seems fortran cannot express the type signature >> of mathematical functions because arguments are >> passed by reference. >> >> so there is no way to declare math interfaces >> and then add omp declare simd to them to get >> simd versions. > > Not even with BIND(C) or so? > >> (it's not clear to me how omp declare simd is >> supposed to work in fortran, but it is not useful >> for vectorizing loops with math functions.) >> >> so gfortran will need a different mechanism to >> do the vectorization, e.g. an option like >> -mveclibabi=glibc, but the list of available >> vector functions need to be specified somewhere. > > I think most useful would be if glibc ships with a > fortran module that provides the appropriate > declarations and users can simply include such > module or such module would be automagically > included like we have that C pre-def header. > > Of course first we have to solve the mystery > of how to actually declare the libm sin() function > in the appropriate way. And if there isn't one, > provide one by means of an extension > ($! pragma gcc whatever?) Always good to CC fortran@ for such stuff anyways. Richard. > Richard.
Re: libmvec simd math functions in fortran
On Thu, Nov 02, 2017 at 12:49:18PM +, Szabolcs Nagy wrote: > to answer my question.. > > it seems fortran cannot express the type signature > of mathematical functions because arguments are > passed by reference. Fortran indeed normally does pass by reference, and you can vectorize it that way (you can use various declare simd clauses to express in detail how the arguments should be handled), or pass by reference: interface double precision function foo (x, y) double precision, value :: x, y !$omp declare simd (foo) end function end interface and/or using bind(c): interface function bar (x, y) bind(C) use, intrinsic :: iso_c_binding real(c_double), value :: x, y real(c_double) :: bar !$omp declare simd (bar) end function end interface etc. Jakub
Re: libmvec simd math functions in fortran
On Thu, Nov 2, 2017 at 1:49 PM, Szabolcs Nagywrote: > On 01/11/17 16:47, Szabolcs Nagy wrote: >> On 01/11/17 16:26, Jakub Jelinek wrote: >>> On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: is there a way to get vectorized math functions in fortran? in c code there is attribute simd declarations or openmp declare simd pragma to tell the compiler which functions have simd variant, but i see no such thing in fortran. >>> >>> !$omp declare simd should work fine in fortran (with -fopenmp >>> or -fopenmp-simd). >>> >> >> 1) i don't want to change the fortran. >> >> 2) it does not work for me. >> >> i want this to call vector powf in libmvec: >> >> subroutine foo(a,b,c) >> real(4) a(8000),b(8000),c(8000) >> do j=1,8000 >> a(j)=b(j)**c(j) >> end do >> end >> >> where do i put >> >> !$omp declare simd (powf) >> >> ? > > to answer my question.. > > it seems fortran cannot express the type signature > of mathematical functions because arguments are > passed by reference. > > so there is no way to declare math interfaces > and then add omp declare simd to them to get > simd versions. Not even with BIND(C) or so? > (it's not clear to me how omp declare simd is > supposed to work in fortran, but it is not useful > for vectorizing loops with math functions.) > > so gfortran will need a different mechanism to > do the vectorization, e.g. an option like > -mveclibabi=glibc, but the list of available > vector functions need to be specified somewhere. I think most useful would be if glibc ships with a fortran module that provides the appropriate declarations and users can simply include such module or such module would be automagically included like we have that C pre-def header. Of course first we have to solve the mystery of how to actually declare the libm sin() function in the appropriate way. And if there isn't one, provide one by means of an extension ($! pragma gcc whatever?) Richard.
Re: libmvec simd math functions in fortran
On 01/11/17 16:47, Szabolcs Nagy wrote: > On 01/11/17 16:26, Jakub Jelinek wrote: >> On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: >>> is there a way to get vectorized math functions in fortran? >>> >>> in c code there is attribute simd declarations or openmp >>> declare simd pragma to tell the compiler which functions >>> have simd variant, but i see no such thing in fortran. >> >> !$omp declare simd should work fine in fortran (with -fopenmp >> or -fopenmp-simd). >> > > 1) i don't want to change the fortran. > > 2) it does not work for me. > > i want this to call vector powf in libmvec: > > subroutine foo(a,b,c) > real(4) a(8000),b(8000),c(8000) > do j=1,8000 > a(j)=b(j)**c(j) > end do > end > > where do i put > > !$omp declare simd (powf) > > ? to answer my question.. it seems fortran cannot express the type signature of mathematical functions because arguments are passed by reference. so there is no way to declare math interfaces and then add omp declare simd to them to get simd versions. (it's not clear to me how omp declare simd is supposed to work in fortran, but it is not useful for vectorizing loops with math functions.) so gfortran will need a different mechanism to do the vectorization, e.g. an option like -mveclibabi=glibc, but the list of available vector functions need to be specified somewhere.
Re: libmvec simd math functions in fortran
On 01/11/17 16:26, Jakub Jelinek wrote: > On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: >> is there a way to get vectorized math functions in fortran? >> >> in c code there is attribute simd declarations or openmp >> declare simd pragma to tell the compiler which functions >> have simd variant, but i see no such thing in fortran. > > !$omp declare simd should work fine in fortran (with -fopenmp > or -fopenmp-simd). > 1) i don't want to change the fortran. 2) it does not work for me. i want this to call vector powf in libmvec: subroutine foo(a,b,c) real(4) a(8000),b(8000),c(8000) do j=1,8000 a(j)=b(j)**c(j) end do end where do i put !$omp declare simd (powf) ?
Re: libmvec simd math functions in fortran
On Wed, Nov 01, 2017 at 04:23:11PM +, Szabolcs Nagy wrote: > is there a way to get vectorized math functions in fortran? > > in c code there is attribute simd declarations or openmp > declare simd pragma to tell the compiler which functions > have simd variant, but i see no such thing in fortran. !$omp declare simd should work fine in fortran (with -fopenmp or -fopenmp-simd). Jakub