Skip to content

Commit

Permalink
Integrate MAGMA
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlucf22 committed Aug 23, 2018
1 parent 8c3fcb8 commit ceadde1
Show file tree
Hide file tree
Showing 35 changed files with 863 additions and 64 deletions.
41 changes: 36 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ if(BML_GPU)
elseif(MAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(OpenMP_CXX_OFFLOAD_FLAG "-fopenmp-targets=nvptx64-nvidia-cuda --cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")
endif()

check_cxx_compiler_flag("${OpenMP_CXX_OFFLOAD_FLAG}" OFFLOAD_CXX_FOUND)
if(OFFLOAD_CXX_FOUND)
message(STATUS "Found GPU offload flags")
Expand All @@ -187,16 +186,36 @@ if(BML_GPU)
message("CUDA CUBLAS libraries: ${CUDA_CUBLAS_LIBRARIES}")
endif()

option(BML_CUDA "Whether to compile with CUDA support" FALSE)
set(BML_CUDA FALSE CACHE BOOL "Whether to compile with CUDA support")
if(BML_CUDA)
include(FindCUDA)
endif()
if (CUDA_FOUND)
include_directories(${CUDA_INCLUDE_DIRS})
endif()

set(BML_OPENCL FALSE CACHE BOOL "Whether to compiler with OpenCL support")
if(BML_OPENCL)
include(FindOpenCL)
endif()

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

set(BML_MAGMA FALSE CACHE BOOL "Whether to use MAGMA library")
if(BML_MAGMA)
FIND_PACKAGE(MAGMA)
endif()
if (MAGMA_FOUND)
add_definitions(-DBML_USE_MAGMA)
message(STATUS
"MAGMA was found:\n"
" MAGMA_INCLUDE_DIRS: ${MAGMA_INCLUDE_DIRS}\n"
" MAGMA_LIBRARY_DIRS: ${MAGMA_LIBRARY_DIRS}\n"
" MAGMA_LIBRARIES: ${MAGMA_LIBRARIES}"
)
include_directories(${MAGMA_INCLUDE_DIRS})
endif()

set(BML_INTERNAL_BLAS FALSE
CACHE BOOL "Use internal BLAS functions")

Expand All @@ -214,6 +233,8 @@ if(BLAS_VENDOR STREQUAL "Intel" OR BLAS_VENDOR STREQUAL "MKL")
set(MKLROOT $ENV{MKLROOT})
message(STATUS "Attempting to use Intel's BLAS/LAPACK (MKL)")
if(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
message(STATUS "Look for MKL in ${MKLROOT}")
set(MKLDIR "${MKLROOT}/lib/intel64")
if(BML_OPENMP)
set(BLAS_LIBRARIES "-L${MKLROOT}/lib/intel64"
-lmkl_lapack95_lp64 -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread
Expand Down Expand Up @@ -276,22 +297,24 @@ elseif(BLAS_VENDOR STREQUAL "")
else()
message(FATAL_ERROR "Unknown BLAS_VENDOR ${BLAS_VENDOR}")
endif()
message(STATUS "BLAS_LIBRARIES=${BLAS_LIBRARIES}")

if(NOT (BLAS_LIBRARIES OR BLAS_FOUND OR NOBLAS))
include(FindBLAS)
message(STATUS "BLA_VENDOR=${BLA_VENDOR}")
message(STATUS "BLAS_LIBRARIES=${BLAS_LIBRARIES}")
if(NOT BLAS_FOUND)
message(FATAL_ERROR "Can not find suitable BLAS library")
endif()
endif()

if(NOT (LAPACK_LIBRARIES OR LAPACK_FOUND OR NOBLAS))
if(NOT (LAPACK_LIBRARIES OR LAPACK_FOUND OR NOBLAS OR MAGMA_FOUND))
include(FindLAPACK)
if(NOT LAPACK_FOUND)
message(FATAL_ERROR "Can not find suitable LAPACK library")
endif()
endif()

message(STATUS "BLAS_LIBRARIES=${BLAS_LIBRARIES}")
include(CheckFunctionExists)
include(CheckFortranFunctionExists)

Expand All @@ -307,10 +330,17 @@ if(NOT (BLAS_FOUND OR NOBLAS))
endif()

if(NOT (LAPACK_FOUND OR NOBLAS))
message(FATAL_ERROR "Could not find LAPACK library.")
if(NOT (MAGMA_FOUND) )
message(FATAL_ERROR "Could not find LAPACK library.")
endif()
endif()

include(${CMAKE_SOURCE_DIR}/cmake/bmlCheckCFortranFunctionExists.cmake)
if(MAGMA_FOUND)
set(CMAKE_REQUIRED_LIBRARIES
${MAGMA_LIBRARIES})
list(APPEND LINK_LIBRARIES ${MAGMA_LIBRARIES})
endif()
if(BLAS_FOUND)
add_definitions(-DHAVE_BLAS)

Expand Down Expand Up @@ -496,6 +526,7 @@ if(OPENMP_FOUND)
message(STATUS "Additional C compiler link flags: '${OpenMP_C_FLAGS}'")
endif()

message(STATUS "LINK_LIBRARIES=${LINK_LIBRARIES}")
string(REPLACE ";" " " LINKLINE "${LINK_LIBRARIES}")
string(STRIP "${LINKLINE}" LINKLINE)
message(STATUS "Link flags: '${LINKLINE}'")
Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ EOF
echo "EXTRA_LINK_FLAGS Add extra link flags (default is '${EXTRA_LINK_FLAGS}')"
echo "BML_GPU {yes,no} (default is ${BML_GPU})"
echo "GPU_ARCH GPU architecture (default is ${GPU_ARCH})"
echo "BML_CUDA Build with CUDA (default is ${BML_CUDA})"
echo "BML_MAGMA Build with MAGMA (default is ${BML_MAGMA})"
echo "CUDA_TOOLKIT_ROOT_DIR Path to CUDA dir (default is ${CUDA_TOOLKIT_ROOT_DIR})"
}

Expand All @@ -84,6 +86,8 @@ set_defaults() {
: ${EXTRA_LINK_FLAGS:=}
: ${BML_GPU:=no}
: ${GPU_ARCH:=}
: ${BML_CUDA:=no}
: ${BML_MAGMA:=no}
: ${CUDA_TOOLKIT_ROOT_DIR:=}
}

Expand Down Expand Up @@ -149,6 +153,8 @@ configure() {
-DCMAKE_VERBOSE_MAKEFILE=${VERBOSE_MAKEFILE} \
-DBML_GPU="${BML_GPU}" \
-DGPU_ARCH="${GPU_ARCH}" \
-DBML_CUDA="${BML_CUDA}" \
-DBML_MAGMA="${BML_MAGMA}" \
-DCUDA_TOOLKIT_ROOT_DIR="${CUDA_TOOLKIT_ROOT_DIR}" \
| tee -a "${LOG_FILE}"
check_pipe_error
Expand Down
36 changes: 36 additions & 0 deletions cmake/FindMAGMA.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# - Find the MAGMA library
#
# Usage:
# find_package(MAGMA [REQUIRED] [QUIET] )
#
# It sets the following variables:
# MAGMA_FOUND ... true if magma is found on the system
# MAGMA_LIBRARY_DIRS ... full path to magma library
# MAGMA_INCLUDE_DIRS ... magma include directory
# MAGMA_LIBRARIES ... magma libraries
#
# The following variables will be checked by the function
# MAGMA_USE_STATIC_LIBS ... if true, only static libraries are found
# MAGMA_ROOT ... if set, the libraries are exclusively searched
# under this path

#If environment variable MAGMA_ROOT is specified, it has same effect as MAGMA_ROOT
if( NOT MAGMA_ROOT AND NOT $ENV{MAGMA_ROOT} STREQUAL "" )
set( MAGMA_ROOT $ENV{MAGMA_ROOT} )
# set library directories
set(MAGMA_LIBRARY_DIRS ${MAGMA_ROOT}/lib)
# set include directories
set(MAGMA_INCLUDE_DIRS ${MAGMA_ROOT}/include)
# set libraries
find_library(
MAGMA_LIBRARIES
NAMES "magma"
PATHS ${MAGMA_ROOT}
PATH_SUFFIXES "lib"
NO_DEFAULT_PATH
)
set(MAGMA_FOUND TRUE)
else()
set(MAGMA_FOUND FALSE)
endif()

23 changes: 23 additions & 0 deletions magma_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
module load gcc/5.4.0
module load netlib-lapack/3.6.1
module load magma/2.2.0
module load cuda/8.0.54
module load cmake/3.6.1

# Make sure all the paths are correct

rm -r build
rm -r install

MY_PATH=$(pwd)

export BML_OPENMP=${BML_OPENMP:=no}
export INSTALL_DIR=${INSTALL_DIR:="${MY_PATH}/install"}
export BML_MAGMA=${BML_MAGMA:=yes}
export MAGMA_ROOT=$OLCF_MAGMA_ROOT

./build.sh testing
#./build.sh install


7 changes: 7 additions & 0 deletions src/C-interface/bml_init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "bml_init.h"
#include "bml_parallel.h"

#ifdef BML_USE_MAGMA
#include "magma_v2.h"
#endif

#include <stdlib.h>

/** Initialize.
Expand All @@ -15,6 +19,9 @@ bml_init(
int *argc,
char ***argv)
{
#ifdef BML_USE_MAGMA
magma_init();
#endif
bml_initParallel(argc, argv);
}

Expand Down
7 changes: 7 additions & 0 deletions src/C-interface/bml_shutdown.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "bml_shutdown.h"
#include "bml_parallel.h"

#ifdef BML_USE_MAGMA
#include "magma_v2.h"
#endif

#include <stdlib.h>

/** Shutdown.
Expand All @@ -13,6 +17,9 @@ bml_shutdown(
)
{
bml_shutdownParallel();
#ifdef BML_USE_MAGMA
magma_finalize();
#endif
}

/** Shutdown from Fortran.
Expand Down
34 changes: 29 additions & 5 deletions src/C-interface/dense/bml_add_dense_typed.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*needs to be included before #include <complex.h>*/
#ifdef BML_USE_MAGMA
#include "magma_v2.h"
#endif

#include "../../macros.h"
#include "../../typed.h"
#include "../blas.h"
Expand Down Expand Up @@ -39,14 +44,23 @@ void TYPED_FUNC(
const double alpha,
const double beta)
{
REAL_T alpha_ = alpha;
REAL_T beta_ = beta;
int myRank = bml_getMyRank();

int nElems = B->domain->localRowExtent[myRank] * B->N;
int startIndex = B->domain->localDispl[myRank];
int inc = 1;

#ifdef BML_USE_MAGMA
nElems = B->N * B->ld;
MAGMA_T alpha__ = MAGMACOMPLEX(MAKE) (alpha, 0.);
MAGMA_T beta__ = MAGMACOMPLEX(MAKE) (beta, 0.);
MAGMA(scal) (nElems, alpha__, A->matrix, inc, A->queue);
MAGMA(axpy) (nElems, beta__, B->matrix, inc,
A->matrix + startIndex, inc, A->queue);
#else
REAL_T alpha_ = alpha;
REAL_T beta_ = beta;

#ifdef NOBLAS
LOG_ERROR("No BLAS library");
#else
Expand All @@ -55,6 +69,7 @@ void TYPED_FUNC(
A->matrix + startIndex, &inc);
#endif

#endif
}

/** Matrix addition and calculate TrNorm.
Expand Down Expand Up @@ -114,10 +129,18 @@ void TYPED_FUNC(
bml_matrix_dense_t * A,
const double beta)
{
REAL_T beta_ = beta;
REAL_T *A_matrix = (REAL_T *) A->matrix;

int N = A->N;
#if BML_USE_MAGMA
MAGMA_T *A_matrix = (MAGMA_T *) A->matrix;
MAGMA_T beta_ = MAGMACOMPLEX(MAKE) (beta, 0.);
bml_matrix_dense_t *B =
TYPED_FUNC(bml_identity_matrix_dense) (N, sequential);
MAGMABLAS(geadd) (N, N, beta_, (MAGMA_T *) B->matrix, B->ld,
A_matrix, A->ld, A->queue);
bml_deallocate_dense(B);
#else
REAL_T *A_matrix = (REAL_T *) A->matrix;
REAL_T beta_ = beta;
int *A_localRowMin = A->domain->localRowMin;
int *A_localRowMax = A->domain->localRowMax;
int myRank = bml_getMyRank();
Expand All @@ -131,6 +154,7 @@ void TYPED_FUNC(
{
A_matrix[ROWMAJOR(i, i, N, N)] += beta_;
}
#endif
}

/** Matrix addition.
Expand Down
8 changes: 8 additions & 0 deletions src/C-interface/dense/bml_allocate_dense.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ void
bml_deallocate_dense(
bml_matrix_dense_t * A)
{
#ifdef BML_USE_MAGMA
magma_queue_destroy(A->queue);
#endif
bml_deallocate_domain(A->domain);
bml_deallocate_domain(A->domain2);
#ifdef BML_USE_MAGMA
magma_int_t ret = magma_free(A->matrix);
assert(ret == MAGMA_SUCCESS);
#else
bml_free_memory(A->matrix);
#endif
bml_free_memory(A);
}

Expand Down
Loading

0 comments on commit ceadde1

Please sign in to comment.