Skip to content

Commit

Permalink
Added wrapper functions for GPU acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-evans committed Jun 10, 2015
1 parent 3055c26 commit 0bb525a
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 42 deletions.
Empty file added hdr/cuda.hpp
Empty file.
29 changes: 29 additions & 0 deletions hdr/gpu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef GPU_H_
#define GPU_H_
//-----------------------------------------------------------------------------
//
// This header file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2015. All rights reserved.
//
//-----------------------------------------------------------------------------

namespace gpu{

//-----------------------------------------------------------------------------
// Variables used for GPU acceleration
//-----------------------------------------------------------------------------
extern bool acceleration; // flag to enable gpu_acceleration

//-----------------------------------------------------------------------------
// Functions for GPU acceleration
//-----------------------------------------------------------------------------
extern void initialize();
extern void llg_heun();
extern void stats_update();
extern void finalize();

} // end of gpu namespace

#endif //GPU_H_
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ obj/qvoronoi/usermem.o\
obj/qvoronoi/userprintf.o\
obj/qvoronoi/userprintf_rbox.o\

# Include supplementary makefiles
include src/gpu/makefile

ICC_OBJECTS=$(OBJECTS:.o=_i.o)
LLVM_OBJECTS=$(OBJECTS:.o=_llvm.o)
Expand Down
Empty file added obj/gpu/.gitignore
Empty file.
26 changes: 26 additions & 0 deletions src/gpu/data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//-----------------------------------------------------------------------------
//
// This source file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2014. All rights reserved.
//
//-----------------------------------------------------------------------------

// C++ standard library headers

// Vampire headers
#include "gpu.hpp"

namespace gpu{

//-----------------------------------------------------------------------------
// Shared variables used for GPU acceleration
//-----------------------------------------------------------------------------
bool acceleration = false; // flag to enable gpu_acceleration

namespace internal{

} // end of internal namespace

} // end of gpu namespace
34 changes: 34 additions & 0 deletions src/gpu/finalize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
//
// This source file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2014. All rights reserved.
//
//-----------------------------------------------------------------------------

// C++ standard library headers

// Vampire headers
#include "gpu.hpp"
#include "cuda.hpp"
#include "errors.hpp"
//#include "opencl.hpp"

namespace gpu{

//----------------------------------------------------------------------------------
// Function to call correct de-initialization function depending on cuda,opencl etc
//----------------------------------------------------------------------------------
void finalize(){

#ifdef CUDA
cuda::finalize();
#elseif OPENCL
opencl::finalize();
#endif

return;
}

} // end of namespace gpu
48 changes: 48 additions & 0 deletions src/gpu/initialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//-----------------------------------------------------------------------------
//
// This source file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2014. All rights reserved.
//
//-----------------------------------------------------------------------------

// C++ standard library headers
#include <iostream>

// Vampire headers
#include "gpu.hpp"
#include "cuda.hpp"
#include "errors.hpp"
#include "vio.hpp"
//#include "opencl.hpp"

namespace gpu{

//-------------------------------------------------------------------------------
// Function to call correct initialization function depending on cuda,opencl etc
//-------------------------------------------------------------------------------
void initialize(){

// flag to check for successful initialization
bool initialized=false;

#ifdef CUDA
initialized = cuda::initialize();
#elseif OPENCL
initialized = opencl::initialize();
#endif

// Check for no initialization
if(initialized == false){
std::cerr << "Error: no support for GPU acceleration, please recompile using -D CUDA compile" << std::endl;
std::cerr << "time flag with cuda development libraries v6.0 or greater." << std::endl;
std::cerr << "Aborting program." << std::endl;
zlog << zTs() << "Error: no support for GPU acceleration, please recompile using -D CUDA compile time flag with cuda development libraries v6.0 or greater." << std::endl;
err::vexit();
}

return;
}

} // end of namespace gpu
34 changes: 34 additions & 0 deletions src/gpu/llg_heun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
//
// This source file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2014. All rights reserved.
//
//-----------------------------------------------------------------------------

// C++ standard library headers

// Vampire headers
#include "gpu.hpp"
#include "cuda.hpp"
#include "errors.hpp"
//#include "opencl.hpp"

namespace gpu{

//--------------------------------------------------------------------------
// Function to call correct llg_heun function depending on cuda,opencl etc
//--------------------------------------------------------------------------
void llg_heun(){

#ifdef CUDA
cuda::llg_heun();
#elseif OPENCL
opencl::llg_heun();
#endif

return;
}

} // end of namespace gpu
14 changes: 14 additions & 0 deletions src/gpu/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#--------------------------------------------------------------
# Makefile for gpu module
#--------------------------------------------------------------

# List module object filenames
gpu_objects=\
data.o \
finalize.o \
initialize.o \
llg_heun.o \
statistics.o

# Append module objects to global tree
OBJECTS+=$(addprefix obj/gpu/,$(gpu_objects))
34 changes: 34 additions & 0 deletions src/gpu/statistics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
//
// This source file is part of the VAMPIRE open source package under the
// GNU GPL (version 2) licence (see licence file for details).
//
// (c) R F L Evans 2014. All rights reserved.
//
//-----------------------------------------------------------------------------

// C++ standard library headers

// Vampire headers
#include "gpu.hpp"
#include "cuda.hpp"
#include "errors.hpp"
//#include "opencl.hpp"

namespace gpu{

//-------------------------------------------------------------------------------
// Function to call correct statistics update function
//-------------------------------------------------------------------------------
void stats_update(){

#ifdef CUDA
cuda::stats_update();
#elseif OPENCL
opencl::stats_update();
#endif

return;
}

} // end of namespace gpu
81 changes: 40 additions & 41 deletions src/simulate/sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "program.hpp"
#include "demag.hpp"
#include "errors.hpp"
#include "gpu.hpp"
#include "material.hpp"
#include "random.hpp"
#include "sim.hpp"
Expand Down Expand Up @@ -158,7 +159,7 @@ namespace sim{
int save_checkpoint_rate=1; // Default increment between checkpoints

// Local function declarations
int integrate_serial(int);
void integrate_serial(int);
int integrate_mpi(int);

// Monte Carlo statistics counters
Expand Down Expand Up @@ -252,6 +253,9 @@ int run(){
// Check for load spin configurations from checkpoint
if(sim::load_checkpoint_flag) load_checkpoint();

// Initialize GPU acceleration if enabled
if(gpu::acceleration) gpu::initialize();

// Select program to run
switch(sim::program){
case 0:
Expand Down Expand Up @@ -417,6 +421,9 @@ int run(){

//program::LLB_Boltzmann();

// De-initialize GPU
gpu::finalize();

// optionally save checkpoint file
if(sim::save_checkpoint_flag && !sim::save_checkpoint_continuous_flag) save_checkpoint();

Expand Down Expand Up @@ -475,35 +482,32 @@ int integrate(int n_steps){
///
/// @section Information
/// @author Richard Evans, [email protected]
/// @version 1.0
/// @date 05/02/2011
/// @version 1.1
/// @date 10/06/2015
///
/// @return EXIT_SUCCESS
///
/// @internal
/// Created: 05/02/2011
/// Revision: ---
///=====================================================================================
///
int integrate_serial(int n_steps){

// Check for calling of function
if(err::check==true) std::cout << "sim::integrate_serial has been called" << std::endl;

// Case statement to call integrator
switch(sim::integrator){
case 0: // LLG Heun
for(int ti=0;ti<n_steps;ti++){
// Select CUDA version if supported
#ifdef CUDA
sim::LLG_Heun_cuda();
#else
sim::LLG_Heun();
#endif
// increment time
increment_time();
}
break;
void integrate_serial(int n_steps){

// Check for calling of function
if(err::check==true) std::cout << "sim::integrate_serial has been called" << std::endl;

// Case statement to call integrator
switch(sim::integrator){

case 0: // LLG Heun
for(int ti=0;ti<n_steps;ti++){
// Optionally select GPU accelerated version
if(gpu::acceleration) gpu::llg_heun();
// Otherwise use CPU version
else sim::LLG_Heun();
// Increment time
increment_time();
}
break;

case 1: // Montecarlo
for(int ti=0;ti<n_steps;ti++){
Expand All @@ -512,20 +516,15 @@ int integrate_serial(int n_steps){
increment_time();
}
break;

case 2: // LLG Midpoint
for(int ti=0;ti<n_steps;ti++){
// Select CUDA version if supported
#ifdef CUDA
sim::LLG_Midpoint_cuda();
#else
sim::LLG_Midpoint();
#endif
// increment time
increment_time();
}
break;


case 2: // LLG Midpoint
for(int ti=0;ti<n_steps;ti++){
sim::LLG_Midpoint();
// increment time
increment_time();
}
break;

case 3: // Constrained Monte Carlo
for(int ti=0;ti<n_steps;ti++){
sim::ConstrainedMonteCarlo();
Expand All @@ -544,11 +543,11 @@ int integrate_serial(int n_steps){

default:{
std::cerr << "Unknown integrator type "<< sim::integrator << " requested, exiting" << std::endl;
exit (EXIT_FAILURE);
}
err::vexit();
}
}

return EXIT_SUCCESS;
return;
}

/// @brief Wrapper function to call MPI parallel integrators
Expand Down
4 changes: 3 additions & 1 deletion src/utility/statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
///
// Headers
#include "atoms.hpp"
#include "gpu.hpp"
#include "material.hpp"
#include "errors.hpp"
#include "vio.hpp"
Expand Down Expand Up @@ -179,7 +180,8 @@ int mag_m(){
}

// update statistics - need to eventually replace mag_m() with stats::update()...
stats::update(atoms::x_spin_array, atoms::y_spin_array, atoms::z_spin_array, atoms::m_spin_array);
if(gpu::acceleration) gpu::stats_update();
else stats::update(atoms::x_spin_array, atoms::y_spin_array, atoms::z_spin_array, atoms::m_spin_array);

// optionally calculate system torque
if(stats::calculate_torque==true) stats::system_torque();
Expand Down

0 comments on commit 0bb525a

Please sign in to comment.