Updating Kokkos lib
git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@15556 f3b2605a-c512-4ea7-a41b-209d697bcdaa
This commit is contained in:
17
lib/kokkos/example/global_2_local_ids/CMakeLists.txt
Normal file
17
lib/kokkos/example/global_2_local_ids/CMakeLists.txt
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
|
||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
SET(SOURCES "")
|
||||
|
||||
SET(SOURCES
|
||||
G2L_Main.cpp
|
||||
)
|
||||
|
||||
TRIBITS_ADD_EXECUTABLE(
|
||||
global_2_local_ids
|
||||
SOURCES ${SOURCES}
|
||||
COMM serial mpi
|
||||
)
|
||||
|
||||
|
||||
266
lib/kokkos/example/global_2_local_ids/G2L.hpp
Normal file
266
lib/kokkos/example/global_2_local_ids/G2L.hpp
Normal file
@ -0,0 +1,266 @@
|
||||
//@HEADER
|
||||
// ************************************************************************
|
||||
//
|
||||
// Kokkos v. 2.0
|
||||
// Copyright (2014) Sandia Corporation
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the Corporation nor the names of the
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
|
||||
//
|
||||
// ************************************************************************
|
||||
//@HEADER
|
||||
|
||||
#ifndef KOKKOS_GLOBAL_TO_LOCAL_IDS_HPP
|
||||
#define KOKKOS_GLOBAL_TO_LOCAL_IDS_HPP
|
||||
|
||||
#include <Kokkos_Core.hpp>
|
||||
|
||||
#include <Kokkos_UnorderedMap.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
#include <impl/Kokkos_Timer.hpp>
|
||||
|
||||
// This test will simulate global ids
|
||||
|
||||
namespace G2L {
|
||||
|
||||
static const unsigned begin_id_size = 256u;
|
||||
static const unsigned end_id_size = 1u << 25;
|
||||
static const unsigned id_step = 2u;
|
||||
|
||||
//use to help generate global ids
|
||||
union helper
|
||||
{
|
||||
uint32_t word;
|
||||
uint8_t byte[4];
|
||||
};
|
||||
|
||||
|
||||
//generate a unique global id from the local id
|
||||
template <typename Device>
|
||||
struct generate_ids
|
||||
{
|
||||
typedef Device execution_space;
|
||||
typedef typename execution_space::size_type size_type;
|
||||
typedef Kokkos::View<uint32_t*,execution_space> local_id_view;
|
||||
|
||||
local_id_view local_2_global;
|
||||
|
||||
generate_ids( local_id_view & ids)
|
||||
: local_2_global(ids)
|
||||
{
|
||||
Kokkos::parallel_for(local_2_global.size(), *this);
|
||||
}
|
||||
|
||||
|
||||
KOKKOS_INLINE_FUNCTION
|
||||
void operator()(size_type i) const
|
||||
{
|
||||
|
||||
helper x = {static_cast<uint32_t>(i)};
|
||||
|
||||
// shuffle the bytes of i to create a unique, semi-random global_id
|
||||
x.word = ~x.word;
|
||||
|
||||
uint8_t tmp = x.byte[3];
|
||||
x.byte[3] = x.byte[1];
|
||||
x.byte[1] = tmp;
|
||||
|
||||
tmp = x.byte[2];
|
||||
x.byte[2] = x.byte[0];
|
||||
x.byte[0] = tmp;
|
||||
|
||||
local_2_global[i] = x.word;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// fill a map of global_id -> local_id
|
||||
template <typename Device>
|
||||
struct fill_map
|
||||
{
|
||||
typedef Device execution_space;
|
||||
typedef typename execution_space::size_type size_type;
|
||||
typedef Kokkos::View<const uint32_t*,execution_space, Kokkos::MemoryRandomAccess> local_id_view;
|
||||
typedef Kokkos::UnorderedMap<uint32_t,size_type,execution_space> global_id_view;
|
||||
|
||||
global_id_view global_2_local;
|
||||
local_id_view local_2_global;
|
||||
|
||||
fill_map( global_id_view gIds, local_id_view lIds)
|
||||
: global_2_local(gIds) , local_2_global(lIds)
|
||||
{
|
||||
Kokkos::parallel_for(local_2_global.size(), *this);
|
||||
}
|
||||
|
||||
KOKKOS_INLINE_FUNCTION
|
||||
void operator()(size_type i) const
|
||||
{
|
||||
global_2_local.insert( local_2_global[i], i);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// check that the global id is found and that it maps to the local id
|
||||
template <typename Device>
|
||||
struct find_test
|
||||
{
|
||||
typedef Device execution_space;
|
||||
typedef typename execution_space::size_type size_type;
|
||||
typedef Kokkos::View<const uint32_t*,execution_space, Kokkos::MemoryRandomAccess> local_id_view;
|
||||
typedef Kokkos::UnorderedMap<const uint32_t, const size_type,execution_space> global_id_view;
|
||||
|
||||
global_id_view global_2_local;
|
||||
local_id_view local_2_global;
|
||||
|
||||
typedef size_t value_type;
|
||||
|
||||
find_test( global_id_view gIds, local_id_view lIds, value_type & num_errors)
|
||||
: global_2_local(gIds) , local_2_global(lIds)
|
||||
{
|
||||
Kokkos::parallel_reduce(local_2_global.size(), *this, num_errors);
|
||||
}
|
||||
|
||||
KOKKOS_INLINE_FUNCTION
|
||||
void init(value_type & v) const
|
||||
{ v = 0; }
|
||||
|
||||
KOKKOS_INLINE_FUNCTION
|
||||
void join(volatile value_type & dst, volatile value_type const & src) const
|
||||
{ dst += src; }
|
||||
|
||||
KOKKOS_INLINE_FUNCTION
|
||||
void operator()(size_type i, value_type & num_errors) const
|
||||
{
|
||||
uint32_t index = global_2_local.find( local_2_global[i] );
|
||||
|
||||
if ( !global_2_local.valid_at(index)
|
||||
|| global_2_local.key_at(index) != local_2_global[i]
|
||||
|| global_2_local.value_at(index) != i)
|
||||
++num_errors;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// run test
|
||||
template <typename Device>
|
||||
size_t test_global_to_local_ids(unsigned num_ids, unsigned capacity, unsigned num_find_iterations)
|
||||
{
|
||||
|
||||
typedef Device execution_space;
|
||||
typedef typename execution_space::size_type size_type;
|
||||
|
||||
typedef Kokkos::View<uint32_t*,execution_space> local_id_view;
|
||||
typedef Kokkos::UnorderedMap<uint32_t,size_type,execution_space> global_id_view;
|
||||
|
||||
double elasped_time = 0;
|
||||
Kokkos::Timer timer;
|
||||
|
||||
local_id_view local_2_global("local_ids", num_ids);
|
||||
global_id_view global_2_local(capacity);
|
||||
|
||||
int shiftw = 15;
|
||||
|
||||
//create
|
||||
elasped_time = timer.seconds();
|
||||
std::cout << std::setw(shiftw) << "allocate: " << elasped_time << std::endl;
|
||||
timer.reset();
|
||||
|
||||
// generate unique ids
|
||||
{
|
||||
generate_ids<Device> gen(local_2_global);
|
||||
}
|
||||
|
||||
// generate
|
||||
elasped_time = timer.seconds();
|
||||
std::cout << std::setw(shiftw) << "generate: " << elasped_time << std::endl;
|
||||
timer.reset();
|
||||
|
||||
{
|
||||
fill_map<Device> fill(global_2_local, local_2_global);
|
||||
}
|
||||
|
||||
// fill
|
||||
elasped_time = timer.seconds();
|
||||
std::cout << std::setw(shiftw) << "fill: " << elasped_time << std::endl;
|
||||
timer.reset();
|
||||
|
||||
|
||||
size_t num_errors = global_2_local.failed_insert();
|
||||
|
||||
if (num_errors == 0u) {
|
||||
for (unsigned i=0; i<num_find_iterations; ++i)
|
||||
{
|
||||
find_test<Device> find(global_2_local, local_2_global,num_errors);
|
||||
}
|
||||
|
||||
// find
|
||||
elasped_time = timer.seconds();
|
||||
std::cout << std::setw(shiftw) << "lookup: " << elasped_time << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << " !!! Fill Failed !!!" << std::endl;
|
||||
}
|
||||
|
||||
return num_errors;
|
||||
}
|
||||
|
||||
template <typename Device>
|
||||
size_t run_test(unsigned num_ids, unsigned num_find_iterations)
|
||||
{
|
||||
// expect to fail
|
||||
unsigned capacity = (num_ids*2u)/3u;
|
||||
std::cout << " 66% of needed capacity (should fail)" << std::endl;
|
||||
test_global_to_local_ids<Device>(num_ids, capacity, num_find_iterations);
|
||||
|
||||
//should not fail
|
||||
std::cout << " 100% of needed capacity" << std::endl;
|
||||
capacity = num_ids;
|
||||
size_t num_errors = test_global_to_local_ids<Device>(num_ids, capacity, num_find_iterations);
|
||||
|
||||
//should not fail
|
||||
std::cout << " 150% of needed capacity" << std::endl;
|
||||
capacity = (num_ids*3u)/2u;
|
||||
num_errors += test_global_to_local_ids<Device>(num_ids, capacity, num_find_iterations);
|
||||
|
||||
return num_errors;
|
||||
}
|
||||
|
||||
|
||||
} // namespace G2L
|
||||
|
||||
|
||||
#endif //KOKKOS_GLOBAL_TO_LOCAL_IDS_HPP
|
||||
|
||||
149
lib/kokkos/example/global_2_local_ids/G2L_Main.cpp
Normal file
149
lib/kokkos/example/global_2_local_ids/G2L_Main.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
//@HEADER
|
||||
// ************************************************************************
|
||||
//
|
||||
// Kokkos v. 2.0
|
||||
// Copyright (2014) Sandia Corporation
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the Corporation nor the names of the
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
|
||||
//
|
||||
// ************************************************************************
|
||||
//@HEADER
|
||||
*/
|
||||
|
||||
#include <Kokkos_Core.hpp>
|
||||
|
||||
#include <G2L.hpp>
|
||||
|
||||
namespace G2L {
|
||||
|
||||
size_t run_serial(unsigned num_ids, unsigned num_find_iterations)
|
||||
{
|
||||
#ifdef KOKKOS_HAVE_SERIAL
|
||||
std::cout << "Serial" << std::endl;
|
||||
return run_test<Kokkos::Serial>(num_ids,num_find_iterations);
|
||||
#else
|
||||
return 0;
|
||||
#endif // KOKKOS_HAVE_SERIAL
|
||||
}
|
||||
|
||||
size_t run_threads(unsigned num_ids, unsigned num_find_iterations)
|
||||
{
|
||||
#ifdef KOKKOS_HAVE_PTHREAD
|
||||
std::cout << "Threads" << std::endl;
|
||||
return run_test<Kokkos::Threads>(num_ids,num_find_iterations);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t run_openmp(unsigned num_ids, unsigned num_find_iterations)
|
||||
{
|
||||
#ifdef KOKKOS_HAVE_OPENMP
|
||||
std::cout << "OpenMP" << std::endl;
|
||||
return run_test<Kokkos::OpenMP>(num_ids,num_find_iterations);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t run_cuda(unsigned num_ids, unsigned num_find_iterations)
|
||||
{
|
||||
#ifdef KOKKOS_HAVE_CUDA
|
||||
std::cout << "Cuda" << std::endl;
|
||||
return run_test<Kokkos::Cuda>(num_ids,num_find_iterations);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace G2L
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned num_ids = 100000;
|
||||
unsigned num_find_iterations = 1000;
|
||||
|
||||
if (argc == 3) {
|
||||
num_ids = atoi(argv[1]);
|
||||
num_find_iterations = atoi(argv[2]);
|
||||
}
|
||||
else if (argc != 1) {
|
||||
std::cout << argv[0] << " num_ids num_find_iterations" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// query the topology of the host
|
||||
unsigned threads_count = 4 ;
|
||||
|
||||
if (Kokkos::hwloc::available()) {
|
||||
threads_count = Kokkos::hwloc::get_available_numa_count() *
|
||||
Kokkos::hwloc::get_available_cores_per_numa() *
|
||||
Kokkos::hwloc::get_available_threads_per_core();
|
||||
|
||||
}
|
||||
|
||||
std::cout << "Threads: " << threads_count << std::endl;
|
||||
std::cout << "Number of ids: " << num_ids << std::endl;
|
||||
std::cout << "Number of find iterations: " << num_find_iterations << std::endl;
|
||||
|
||||
size_t num_errors = 0;
|
||||
|
||||
num_errors += G2L::run_serial(num_ids,num_find_iterations);
|
||||
|
||||
#ifdef KOKKOS_HAVE_CUDA
|
||||
Kokkos::HostSpace::execution_space::initialize(threads_count);
|
||||
Kokkos::Cuda::initialize( Kokkos::Cuda::SelectDevice(0) );
|
||||
num_errors += G2L::run_cuda(num_ids,num_find_iterations);
|
||||
Kokkos::Cuda::finalize();
|
||||
Kokkos::HostSpace::execution_space::finalize();
|
||||
#endif
|
||||
|
||||
#ifdef KOKKOS_HAVE_PTHREAD
|
||||
Kokkos::Threads::initialize( threads_count );
|
||||
num_errors += G2L::run_threads(num_ids,num_find_iterations);
|
||||
Kokkos::Threads::finalize();
|
||||
#endif
|
||||
|
||||
#ifdef KOKKOS_HAVE_OPENMP
|
||||
Kokkos::OpenMP::initialize( threads_count );
|
||||
num_errors += G2L::run_openmp(num_ids,num_find_iterations);
|
||||
Kokkos::OpenMP::finalize();
|
||||
#endif
|
||||
|
||||
|
||||
return num_errors;
|
||||
}
|
||||
|
||||
53
lib/kokkos/example/global_2_local_ids/Makefile
Normal file
53
lib/kokkos/example/global_2_local_ids/Makefile
Normal file
@ -0,0 +1,53 @@
|
||||
KOKKOS_PATH ?= ../..
|
||||
|
||||
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
|
||||
SRC_DIR := $(dir $(MAKEFILE_PATH))
|
||||
|
||||
SRC = $(wildcard $(SRC_DIR)/*.cpp)
|
||||
OBJ = $(SRC:$(SRC_DIR)/%.cpp=%.o)
|
||||
|
||||
#SRC = $(wildcard *.cpp)
|
||||
#OBJ = $(SRC:%.cpp=%.o)
|
||||
|
||||
default: build
|
||||
echo "Start Build"
|
||||
|
||||
# use installed Makefile.kokkos
|
||||
include $(KOKKOS_PATH)/Makefile.kokkos
|
||||
|
||||
ifneq (,$(findstring Cuda,$(KOKKOS_DEVICES)))
|
||||
CXX = $(NVCC_WRAPPER)
|
||||
CXXFLAGS = -I$(SRC_DIR) -O3
|
||||
LINK = $(CXX)
|
||||
LINKFLAGS =
|
||||
EXE = $(addsuffix .cuda, $(shell basename $(SRC_DIR)))
|
||||
#KOKKOS_DEVICES = "Cuda,OpenMP"
|
||||
#KOKKOS_ARCH = "SNB,Kepler35"
|
||||
else
|
||||
CXX = g++
|
||||
CXXFLAGS = -I$(SRC_DIR) -O3
|
||||
LINK = $(CXX)
|
||||
LINKFLAGS =
|
||||
EXE = $(addsuffix .host, $(shell basename $(SRC_DIR)))
|
||||
#KOKKOS_DEVICES = "OpenMP"
|
||||
#KOKKOS_ARCH = "SNB"
|
||||
endif
|
||||
|
||||
DEPFLAGS = -M
|
||||
|
||||
LIB =
|
||||
|
||||
|
||||
build: $(EXE)
|
||||
|
||||
$(EXE): $(OBJ) $(KOKKOS_LINK_DEPENDS)
|
||||
$(LINK) $(KOKKOS_LDFLAGS) $(LINKFLAGS) $(EXTRA_PATH) $(OBJ) $(KOKKOS_LIBS) $(LIB) -o $(EXE)
|
||||
|
||||
clean:
|
||||
rm -f *.a *.o *.cuda *.host
|
||||
|
||||
# Compilation rules
|
||||
|
||||
%.o:$(SRC_DIR)/%.cpp $(KOKKOS_CPP_DEPENDS)
|
||||
$(CXX) $(KOKKOS_CPPFLAGS) $(KOKKOS_CXXFLAGS) $(CXXFLAGS) $(EXTRA_INC) -c $<
|
||||
|
||||
Reference in New Issue
Block a user