create a static library for kernel

create a static library :

module ab
contains
subroutine ab(a, b)
real :: a, b
end subroutine
end module

… then build… ab.lib … ab.mod …omit

it can be used on the host CPU



but create a static library :

module ab
contains
attributes(device) subroutine ab(a, b)
real, device :: a, b
end subroutine
end module

the module can be use but create a static library

then use ab on kernel gpu:


MODULE aabb
use ab
CONTAINS
attributes(global) subroutine abab( a, b, m, )
integer, value :: m
real, device :: a(m), b(m)

call ab(a(1), b(1))

end subroutine
end module



Linking…
nvlink error : Undefined reference to ‘ab_ab_’ in ‘x64\Debug\SourceFile1.obj’
pgnvd-Fatal-Could not spawn c:\program files\pgi\win64/2017/cuda/7.5/bin\nvlink.exe
child process exit with signal 127: c:\program files\pgi\win64\17.10\bin\pgnvd.exe



What’s the matter
can you help me
thank you

Hi eggbaby123456,

Can you post the command you use to build the library as well as what you use to build and link the main program?

I tried a simple test case below, and it worked fine.

PGI$ pgf90 -c -Mcuda testlib.f90
PGI$ ar cvr libtest.lib testlib.obj

"C:\Program Files\PGI\Microsoft Open Tools 14\bin\amd64\link.exe" /LIB testlib.obj libtest.lib /OUT:libtest.lib /NOLOGO /VERBOSE
Replacing testlib.obj
Appending testlib.obj
PGI$ pgf90 -Mcuda -ltest -L. test.f90
PGI$
PGI$ cat testlib.f90
module ab
 contains
 attributes(device) subroutine ab(a, b)
 real :: a, b
 end subroutine
 end module

PGI$ cat test.f90

MODULE aabb
 use ab
 use cudafor
 CONTAINS
 attributes(global) subroutine abab( a, b, m )
 integer, value :: m
 real, device :: a(m), b(m)

 call ab(a(1), b(1))

 end subroutine
 end module

program foo
  use aabb
  use cudafor
   integer m
 real, device, allocatable :: a(:), b(:)
   m = 1024
   allocate(a(m),b(m))
   call abab<<<1,128>>>(a,b,m)
   deallocate(a,b)
 end program foo

-Mat

build the library:
-g -Bstatic -Mbackslash -Mcuda -I"c:\program files\pgi\win64\17.10\include" -I"C:\Program Files\PGI\Microsoft Open Tools 14\include" -I"C:\Program Files (x86)\Windows Kits\10\Include\shared" -I"C:\Program Files (x86)\Windows Kits\10\Include\um" -Minform=warn

build the main program:
-g -Bstatic -Mbackslash -Mcuda=rdc -I"c:\program files\pgi\win64\17.10\include" -I"C:\Program Files\PGI\Microsoft Open Tools 14\include" -I"C:\Program Files (x86)\Windows Kits\10\Include\shared" -I"C:\Program Files (x86)\Windows Kits\10\Include\um" -Minform=warn

link the main program:
-g -Bstatic -Mcuda=rdc -o “c:\Users\nhri-gpu\documents\visual studio 2015\Projects\PVFProject1\PVFProject1\x64\Debug\PVFProject1.exe” -Wl,/libpath:“C:\Program Files\PGI\win64\17.10\lib” ab.lib

I don’t see where you’re creating the library. This is typically done with the “ar” utility (which is a convenience wrapper for the MS “link.exe” utility), as I show above.

If you are using PVF, then create a new project and select “PGI Visual Fortran -> x64 -> Static Library”. Add your code to this project. When you build, the library will be created having the same name as the project.

Then in a separate project (PGI Visual Fortran -> x64 -> Console Application), you would add your main source, then from the property pages (Link->Command Line), add the library to the link. You would set the path the library in the property pages as well (Link-> General -> Additional Library Directories).

Hope this helps,
Mat

module ab
contains
subroutine ab(a, b)
real :: a, b
end subroutine

attributes(device) subroutine ac(a, b)
real :: a, b
end subroutine

end module

This is created as a Static library





then :…like this :


MODULE aabb
use ab
use cudafor
CONTAINS
attributes(global) subroutine abab( a, b, m )
integer, value :: m
real, device :: a(m), b(m)

!!! call ac(a(1), b(1))
end subroutine


subroutine ccc( a, b, m )
integer, value :: m
real :: a(m), b(m)

call ab(a(1), b(1))

end subroutine


end module

program foo
use aabb
use cudafor
integer m
real, device, allocatable :: a(:), b(:)
m = 1024
allocate(a(m),b(m))
call abab<<<1,128>>>(a,b,m)
deallocate(a,b)
end program foo



------ Build started: Project: PVFProject2, Configuration: Debug x64 ------
Compiling Project …
SourceFile1.cuf
Linking…
PVFProject2 build succeeded.

Build log was saved at “file://c:\Users\nhri-gpu\documents\visual studio 2015\Projects\PVFProject2\PVFProject2\x64\Debug\BuildLog.htm”

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


but …like this:

MODULE aabb
use ab
use cudafor
CONTAINS
attributes(global) subroutine abab( a, b, m )
integer, value :: m
real, device :: a(m), b(m)

call ac(a(1), b(1))
end subroutine


subroutine ccc( a, b, m )
integer, value :: m
real :: a(m), b(m)

call ab(a(1), b(1))

end subroutine



end module

program foo
use aabb
use cudafor
integer m
real, device, allocatable :: a(:), b(:)
m = 1024
allocate(a(m),b(m))
call abab<<<1,128>>>(a,b,m)
deallocate(a,b)
end program foo



Compiling Project …
SourceFile1.cuf
Linking…
nvlink error : Undefined reference to ‘ab_ac_’ in ‘x64\Debug\SourceFile1.obj’
pgnvd-Fatal-Could not spawn c:\program files\pgi\win64/2017/cuda/7.5/bin\nvlink.exe
child process exit with signal 127: c:\program files\pgi\win64\17.10\bin\pgnvd.exe
PVFProject2 build failed.
Build log was saved at “file://c:\Users\nhri-gpu\documents\visual studio 2015\Projects\PVFProject2\PVFProject2\x64\Debug\BuildLog.htm”

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


The reason is that the attributes(device) subroutine can not be use in the Static library .

Here’s my PVF logs (the .htm files) from building your code. I was able to recreate your error if I didn’t quote the full path to the library since it has a space in it. With the quotes, it linked fine. My best guess, something similar is happening with your link. Either you’ve not added the library to the link, or the full path has spaces and hence you need to put quotes around the path.


Building the library:

PVF Build Log

Compiling Project ...

Lib.f90

c:\program files\pgi\win64\17.10\bin\pgfortran.exe -Hx,123,8 -Hx,123,0x40000 -Hx,0,0x40000000 -Mx,0,0x40000000 -Hx,0,0x20000000 -g -Bstatic -Mbackslash -Mcuda -Mfree -I"c:\program files\pgi\win64\17.10\include" -I"C:\Program Files\PGI\Microsoft Open Tools 14\include" -I"C:\Program Files (x86)\Windows Kits\10\Include\shared" -I"C:\Program Files (x86)\Windows Kits\10\Include\um" -Minform=warn -module "x64\Debug" -o "x64\Debug\Lib.obj" -c "c:\users\mcolgrove\documents\visual studio 2015\Projects\ablib\ablib\Lib.f90"

Command exit code: 0


Creating Library...

C:\Program Files\PGI\Microsoft Open Tools 14\bin\amd64\LIB.EXE /OUT:"c:\Users\mcolgrove\documents\visual studio 2015\Projects\ablib\ablib\x64\Debug\ablib.lib" "x64\Debug\Lib.obj" 

Command exit code: 0

ablib build succeeded.

Main program build:

PVF Build Log

Begin rebuild: clean project

Deleting intermediate and output files for project 'abprog', configuration 'Debug'

Compiling Project ...

ConsoleApp.f90

c:\program files\pgi\win64\17.10\bin\pgfortran.exe -Hx,123,8 -Hx,123,0x40000 -Hx,0,0x40000000 -Mx,0,0x40000000 -Hx,0,0x20000000 -g -Bstatic -Mbackslash -Mcuda -Mfree -I"C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug" -I"c:\program files\pgi\win64\17.10\include" -I"C:\Program Files\PGI\Microsoft Open Tools 14\include" -I"C:\Program Files (x86)\Windows Kits\10\Include\shared" -I"C:\Program Files (x86)\Windows Kits\10\Include\um" -Minform=warn -module "x64\Debug" -o "x64\Debug\ConsoleApp.obj" -c "c:\users\mcolgrove\documents\visual studio 2015\Projects\PVFProject4\PVFProject4\ConsoleApp.f90"

Command exit code: 0


Linking...

c:\program files\pgi\win64\17.10\bin\pgfortran.exe -Wl,/libpath:"c:\program files\pgi\win64\17.10\lib" -Wl,/libpath:"C:\Program Files\PGI\Microsoft Open Tools 14\lib\amd64" -Wl,/libpath:"C:\Program Files (x86)\Windows Kits\10\Lib\winv6.3\um\x64" -Yl,"C:\Program Files\PGI\Microsoft Open Tools 14\bin\amd64" -g -Bstatic -Mcuda -o "c:\users\mcolgrove\documents\visual studio 2015\Projects\PVFProject4\PVFProject4\x64\Debug\abprog.exe" -Wl,/libpath:"C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug" "C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug\ablib.lib" "x64\Debug\ConsoleApp.obj" 

Command exit code: 0

abprog build succeeded.

Here’s an example of the link without the library path quoted.

Linking...

c:\program files\pgi\win64\17.10\bin\pgfortran.exe -Wl,/libpath:"c:\program files\pgi\win64\17.10\lib" -Wl,/libpath:"C:\Program Files\PGI\Microsoft Open Tools 14\lib\amd64" -Wl,/libpath:"C:\Program Files (x86)\Windows Kits\10\Lib\winv6.3\um\x64" -Yl,"C:\Program Files\PGI\Microsoft Open Tools 14\bin\amd64" -g -Bstatic -Mcuda -o "c:\users\mcolgrove\documents\visual studio 2015\Projects\PVFProject4\PVFProject4\x64\Debug\abprog.exe" -Wl,/libpath:"C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug" C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug\ablib.lib "x64\Debug\ConsoleApp.obj" 

Command exit code: 2

Command output: [nvlink error : Undefined reference to 'ab_ab_' in 'x64\Debug\ConsoleApp.obj' pgnvd-Fatal-Could not spawn c:\program files\pgi\win64/2017/cuda/7.5/bin\nvlink.exe child process exit with signal 127: c:\program files\pgi\win64\17.10\bin\pgnvd.exe ]

abprog build failed

.

How to quote the full path to the library.
The full path has spaces ??? what does it mean

From the example above, here’s the full path to the library in the failing case:

C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug\ablib.lib

Note that there are spaces in the path name, i.e. “Visual Studio 2015”. The linker does not treat this as a single string, rather it will break it into separate strings and hence cannot find the library. Since it can’t find the library, then you get the undefined reference errors.

Assuming that this is the same problem that you’re encountering, the solution is to quote the full path so it’s treated as a single string:

“C:\Users\mcolgrove\Documents\Visual Studio 2015\Projects\ablib\ablib\x64\Debug\ablib.lib”

Another possibility is that the linker can’t find the library. Just putting “ab.lib” on the link line wont work. You need to use the full path to the library or add a “-L” so the linker can find it.

-Mat

Thank you


I hope to create static library in the same way for host code or

device code in future.