[Trilinos-Users] Trilinos-Kokkos with CUDA

Ashesh Chattopadhyay ashesh6810 at gmail.com
Tue Nov 1 11:25:31 EDT 2016


Hi,

I've built Trilinos with CUDA support and am trying to compile a simple
program with CUDA.

 #include <limits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/time.h>

#include <Kokkos_Core.hpp>

void checkSizes(int &N, int &M, int &S, int &nrepeat);

int main(int argc, char* argv[])
{

  int N = 1024 ;       // number of rows 2^12
  int M = 1024 ;       // number of columns 2^10
  int S = 1024*1024 ;      // total size 2^22
  int nrepeat = 100 ;    // number of repeats of the test






  Kokkos::initialize(argc,argv);

  // typedef Kokkos::Serial   ExecSpace ;
  // typedef Kokkos::Threads  ExecSpace ;
  // typedef Kokkos::OpenMP   ExecSpace ;
  typedef Kokkos::Cuda        ExecSpace ;

  //EXERCISE: Choose device memory space
  // typedef Kokkos::HostSpace    MemSpace;
  // typedef Kokkos::OpenMP       MemSpace;
  typedef Kokkos::CudaSpace       MemSpace;
  // typedef Kokkos::CudaUVMSpace MemSpace;

  typedef Kokkos::LayoutLeft   Layout ;
  // typedef Kokkos::LayoutRight  Layout ;

  typedef Kokkos::RangePolicy<ExecSpace> range_policy ;

  // Allocate y, x vectors and Matrix A:
  // Device
  typedef Kokkos::View<double*, Layout, MemSpace>   ViewVectorType;
  typedef Kokkos::View<double**, Layout, MemSpace>   ViewMatrixType;
  ViewVectorType devy("devy", N);
  ViewVectorType devx("devx", M);
  ViewMatrixType devA("devA", N, M);

  //Host mirror
  ViewVectorType::HostMirror y =  Kokkos::create_mirror_view(devy);
  ViewVectorType::HostMirror x =  Kokkos::create_mirror_view(devx);
  ViewMatrixType::HostMirror A =  Kokkos::create_mirror_view(devA);

  // Initialize y vector on host
  for (int i = 0; i < N; ++i) {
    y( i ) = 1;
  }

  // Initialize x vector on host
  for (int i = 0; i < M; ++i) {
    x( i ) = 1;
  }

  // Initialize A matrix, note 2D indexing computation on host
  for (int j = 0; j < N; ++j) {
    for ( int i = 0 ; i < M ; ++i ) {
      A( j , i ) = 1;
    }
  }

  //Deep copy host view to device views
  Kokkos::deep_copy(devy, y);
  Kokkos::deep_copy(devx, x);
  Kokkos::deep_copy(devA, A);

  // Timer products
  struct timeval begin,end;

  gettimeofday(&begin,NULL);

  for ( int repeat = 0; repeat < nrepeat; repeat++) {

    //Application: <y,Ax> = y^T*A*x
    double result = 0;
    Kokkos::parallel_reduce( range_policy( 0, N ), KOKKOS_LAMBDA ( int j,
double &update ) {
      double temp2 = 0;
      for ( int i = 0 ; i < M ; ++i ) {
        temp2 += devA( j , i ) * devx( i );
      }
      update += devy( j ) * temp2;
    }, result );

    //Output result
    if ( repeat == (nrepeat - 1) )
      printf("  Computed result for %d x %d is %lf\n", N, M, result);
    const double solution = (double)N *(double)M;

    if ( result != solution ) {
      printf("  Error: result( %lf ) != solution( %lf )\n",result,solution);
    }
  }

  gettimeofday(&end,NULL);

  // Calculate time
  double time = 1.0*(end.tv_sec-begin.tv_sec) +
                1.0e-6*(end.tv_usec-begin.tv_usec);

  // Calculate bandwidth.
  // Each matrix A row (each of length M) is read once.
  // The x vector (of length M) is read N times.
  // The y vector (of length N) is read once.
  // double Gbytes = 1.0e-9 * double(sizeof(double) * ( 2 * M * N + N ));
  double Gbytes = 1.0e-9 * double(sizeof(double) * ( M + M * N + N )) ;

  // Print results (problem size, time and bandwidth in GB/s)
  printf("  M( %d ) N( %d ) nrepeat ( %d ) problem( %g MB ) time( %g s )
bandwidth( %g GB/s )\n",
         M , N, nrepeat, Gbytes * 1000, time, Gbytes * nrepeat / time );

  Kokkos::finalize();

  return 0 ;
}

However I get compiler errors with cuda. The error is the following.
























































































*[100%] Building CXX object CMakeFiles/trilinosExample.dir/ex1.cpp.oIn file
included from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/impl/Kokkos_Atomic_View.hpp:47:0,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/impl/KokkosExp_ViewMapping.hpp:56,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_View.hpp:385,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Parallel.hpp:52,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Serial.hpp:52,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Core.hpp:53,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:7:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Atomic.hpp:129:1:
error: ‘__device__’ does not name a type __device__
inline ^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Atomic.hpp:138:1:
error: ‘__device__’ does not name a type __device__ inline ^In file
included from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Cuda.hpp:56:0,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Core.hpp:65,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:7:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:574:10:
error: ‘cudaTextureObject_t’ in namespace ‘::’ does not name a type
static ::cudaTextureObject_t
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:581:3:
error: ‘cudaTextureObject_t’ in namespace ‘::’ does not name a type
::cudaTextureObject_t   m_tex_obj ;
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:622:3:
error: ‘cudaTextureObject_t’ in namespace ‘::’ does not name a type
::cudaTextureObject_t attach_texture_object()
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:
In constructor
‘Kokkos::Experimental::Impl::SharedAllocationRecord<Kokkos::CudaSpace,
void>::SharedAllocationRecord()’:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:587:44:
error: class
‘Kokkos::Experimental::Impl::SharedAllocationRecord<Kokkos::CudaSpace,
void>’ does not have any field named ‘m_tex_obj’   SharedAllocationRecord()
: RecordBase(), m_tex_obj(0), m_space()
{}
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:
At global
scope:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:665:3:
error: ‘cudaTextureObject_t’ in namespace ‘::’ does not name a type
::cudaTextureObject_t      m_tex_obj ;
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:708:3:
error: ‘cudaTextureObject_t’ in namespace ‘::’ does not name a type
::cudaTextureObject_t attach_texture_object()
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:
In constructor
‘Kokkos::Experimental::Impl::SharedAllocationRecord<Kokkos::CudaUVMSpace,
void>::SharedAllocationRecord()’:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_CudaSpace.hpp:671:44:
error: class
‘Kokkos::Experimental::Impl::SharedAllocationRecord<Kokkos::CudaUVMSpace,
void>’ does not have any field named ‘m_tex_obj’   SharedAllocationRecord()
: RecordBase(), m_tex_obj(0), m_space()
{}                                            ^In file included from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Core.hpp:65:0,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:7:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Cuda.hpp:
At global
scope:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Cuda.hpp:212:3:
error: ‘cudaStream_t’ does not name a type   cudaStream_t cuda_stream()
const { return m_stream ; }
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Cuda.hpp:220:3:
error: ‘cudaStream_t’ does not name a type   cudaStream_t m_stream ;   ^In
file included from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_CudaExec.hpp:56:0,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Cuda.hpp:260,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Kokkos_Core.hpp:65,
from
/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:7:/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:56:33:
error: variable or field ‘cuda_internal_error_throw’ declared void void
cuda_internal_error_throw( cudaError e , const char * name, const char *
file = NULL, const int line = 0 );
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:56:33:
error: ‘cudaError’ was not declared in this
scope/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:56:47:
error: expected primary-expression before ‘const’ void
cuda_internal_error_throw( cudaError e , const char * name, const char *
file = NULL, const int line = 0
);
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:56:66:
error: expected primary-expression before ‘const’ void
cuda_internal_error_throw( cudaError e , const char * name, const char *
file = NULL, const int line = 0
);
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:56:92:
error: expected primary-expression before ‘const’ void
cuda_internal_error_throw( cudaError e , const char * name, const char *
file = NULL, const int line = 0
);
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:58:38:
error: variable or field ‘cuda_internal_safe_call’ declared void inline
void cuda_internal_safe_call( cudaError e , const char * name, const char *
file = NULL, const int line = 0)
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:58:38:
error: ‘cudaError’ was not declared in this
scope/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:58:52:
error: expected primary-expression before ‘const’ inline void
cuda_internal_safe_call( cudaError e , const char * name, const char * file
= NULL, const int line =
0)
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:58:71:
error: expected primary-expression before ‘const’ inline void
cuda_internal_safe_call( cudaError e , const char * name, const char * file
= NULL, const int line =
0)
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/include/Cuda/Kokkos_Cuda_Error.hpp:58:97:
error: expected primary-expression before ‘const’ inline void
cuda_internal_safe_call( cudaError e , const char * name, const char * file
= NULL, const int line =
0)
^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:124:1:
error: expected ‘}’ at end of
input } ^/home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/cmake_cpp_examples/ex1.cpp:124:1:
error: expected ‘}’ at end of inputmake[2]: ***
[CMakeFiles/trilinosExample.dir/ex1.cpp.o] Error 1make[1]: ***
[CMakeFiles/trilinosExample.dir/all] Error 2make: *** [all] Error 2*
It appears that there are some errors referenced from the hpp files. I want
to know if I am making a mistake in linking with CUDA.

Tha CMakeList.txt file I use is attached.





-- 
Research Assistant, Multi-Scale Multi-Physics Computation Lab
University of Texas at El Paso
Texas, El Paso, USA
(+1) 915-355-5013
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://trilinos.org/pipermail/trilinos-users/attachments/20161101/1d0555c8/attachment-0001.html>
-------------- next part --------------

#
# CMake example that uses FIND_PACKAGE(Trilinos ...) to build your C++
# application with Trilinos.  You should know a little bit about CMake
# before reading this example; in particular, you should know how to
# add C++ source files and header files to your project.
#

# You need CMake version >= 2.8 to use this example.
cmake_minimum_required(VERSION 2.8)

# Your "do-configure" script that invokes CMake should set
# TRILINOS_PATH to the path to your Trilinos install.
# You do _not_ need to edit this line.
FIND_PACKAGE(Trilinos PATHS /home/ashesh/Ashesh/Trilinos-Kokkos_GPU/Trilinos/install/lib/cmake/Trilinos ${TRILINOS_PATH})

# If FIND_PACKAGE successfully found your Trilinos install, it will
# set the Boolean flag Trilinos_FOUND.  The following IF statement
# fails with a FATAL_ERROR if Trilinos was not found.  If it _was_
# found, it prints out the values of some Trilinos configuration
# details.  You may find them useful for building your application
# that uses Trilinos.
IF(Trilinos_FOUND)
   MESSAGE("\nFound Trilinos!  Here are the details: ")
   MESSAGE("   Trilinos_DIR = ${Trilinos_DIR}")
   MESSAGE("   Trilinos_VERSION = ${Trilinos_VERSION}")
   MESSAGE("   Trilinos_PACKAGE_LIST = ${Trilinos_PACKAGE_LIST}")
   MESSAGE("   Trilinos_LIBRARIES = ${Trilinos_LIBRARIES}")
   MESSAGE("   Trilinos_INCLUDE_DIRS = ${Trilinos_INCLUDE_DIRS}")
   MESSAGE("   Trilinos_TPL_LIST = ${Trilinos_TPL_LIST}")
   MESSAGE("   Trilinos_TPL_INCLUDE_DIRS = ${Trilinos_TPL_INCLUDE_DIRS}")
   MESSAGE("   Trilinos_TPL_LIBRARIES = ${Trilinos_TPL_LIBRARIES}")
   MESSAGE("   Trilinos_BUILD_SHARED_LIBS = ${Trilinos_BUILD_SHARED_LIBS}")
   MESSAGE("   Trilinos_CXX_COMPILER = ${Trilinos_CXX_COMPILER}")
   MESSAGE("   Trilinos_C_COMPILER = ${Trilinos_C_COMPILER}")
   MESSAGE("   Trilinos_Fortran_COMPILER = ${Trilinos_Fortran_COMPILER}")
   MESSAGE("   Trilinos_CXX_COMPILER_FLAGS = ${Trilinos_CXX_COMPILER_FLAGS}")
   MESSAGE("   Trilinos_C_COMPILER_FLAGS = ${Trilinos_C_COMPILER_FLAGS}")
   MESSAGE("   Trilinos_Fortran_COMPILER_FLAGS =
     ${Trilinos_Fortran_COMPILER_FLAGS}")
   MESSAGE("   Trilinos_LINKER = ${Trilinos_LINKER}")
   MESSAGE("   Trilinos_EXTRA_LD_FLAGS = ${Trilinos_EXTRA_LD_FLAGS}")
   MESSAGE("   Trilinos_AR = ${Trilinos_AR}")
   MESSAGE("End of Trilinos details\n")
ELSE()
  MESSAGE(FATAL_ERROR "Could not find Trilinos!")
ENDIF()

# If you haven't already set the C++ compiler, use the same compiler
# that was used to build Trilinos on your machine.  You do _not_ need
# to edit this line.
IF(NOT CMAKE_CXX_COMPILER)
  MESSAGE("Setting CXX compiler.")
  SET(CMAKE_CXX_COMPILER ${Trilinos_CXX_COMPILER} "-std=c++11")
ENDIF()

# If you haven't already set the C compiler, use the same compiler
# that was used to build Trilinos on your machine.  You do _not_ 
# need to edit this line.
IF(NOT CMAKE_C_COMPILER)
  MESSAGE("Setting C compiler.")
  SET(CMAKE_C_COMPILER ${Trilinos_C_COMPILER})
ENDIF()

#
# EDIT HERE: Replace TrilinosExample with your project's name.
#
project(TrilinosExample)

# 
# EDIT HERE: 
#
# Source file(s) in your project.  Here, we include one source file, 
# main.cpp, which contains the main() function.  You may also want to 
# set HEADERS to the list of header files in your project, if there 
# are any.
#
set(SOURCES 
#cxx_main.cpp 
#aztec_code.cpp
#dense_code.cpp
#transient_code.cpp
#network.cpp
#test_net.cpp
#network_new.cpp
#net_debug.cpp
#net_debugv2.cpp
#net_debugv3.cpp
#net_debugv5.cpp
#net_debugv4.cpp
#net_test_ini.cpp
#net_test_parallel.cpp
#net_test_parallel_v2.cpp
#main.cpp CPPWrapper.cpp a.h
#net_test_tp_v5.cpp gauss.cpp g.h
#kokkos_test.cpp
ex1.cpp
  )

# 
# EDIT HERE:
#
# Add to the header include path the current directory, Trilinos'
# include directory, and the include directories of the third-party
# libraries (TPLs) with which Trilinos was built.
#
include_directories(".")
include_directories(${Trilinos_TPL_INCLUDE_DIRS})
include_directories(${Trilinos_INCLUDE_DIRS})

# Add to the library path Trilinos' library path, and the library
# paths of the third-party libraries (TPLs) with which Trilinos was
# built.  
link_directories(${Trilinos_LIBRARY_DIRS})
link_directories(${Trilinos_TPL_LIBRARY_DIRS})

# 
# EDIT HERE: Change trilinosExample to the name of the executable
# file that you want to build.  This line tells CMake which source
# file(s) to use to build the executable.
#
add_executable(trilinosExample ${SOURCES})

#
# EDIT HERE: Change trilinosExample to the name of the executable
# file, as above.  This line tells the linker to link the executable 
# with the Trilinos libraries, and with the third-party libraries 
# (TPLs) with which Trilinos was built.
#
target_link_libraries(trilinosExample
  ${Trilinos_LIBRARIES} 
  ${Trilinos_TPL_LIBRARIES}
  )



More information about the Trilinos-Users mailing list