Alien-XGBoost
view release on metacpan or search on metacpan
xgboost/cub/experimental/defunct/example_coo_spmv.cu view on Meta::CPAN
/******************************************************************************
* Copyright (c) 2011, Duane Merrill. All rights reserved.
* Copyright (c) 2011-2016, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the NVIDIA CORPORATION nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 NVIDIA CORPORATION 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.
*
******************************************************************************/
/******************************************************************************
* An implementation of COO SpMV using prefix scan to implement a
* reduce-value-by-row strategy
******************************************************************************/
// Ensure printing of CUDA runtime errors to console
#define CUB_STDERR
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <cub/cub.cuh>
#include "coo_graph.cuh"
#include "../test/test_util.h"
using namespace cub;
using namespace std;
/******************************************************************************
* Globals, constants, and typedefs
******************************************************************************/
typedef int VertexId; // uint32s as vertex ids
typedef double Value; // double-precision floating point values
bool g_verbose = false;
int g_timing_iterations = 1;
CachingDeviceAllocator g_allocator;
/******************************************************************************
* Texture referencing
******************************************************************************/
/**
* Templated texture reference type for multiplicand vector
*/
template <typename Value>
struct TexVector
{
// Texture type to actually use (e.g., because CUDA doesn't load doubles as texture items)
typedef typename If<(Equals<Value, double>::VALUE), uint2, Value>::Type CastType;
// Texture reference type
typedef texture<CastType, cudaTextureType1D, cudaReadModeElementType> TexRef;
static TexRef ref;
/**
* Bind textures
*/
static void BindTexture(void *d_in, int elements)
{
cudaChannelFormatDesc tex_desc = cudaCreateChannelDesc<CastType>();
if (d_in)
{
size_t offset;
size_t bytes = sizeof(CastType) * elements;
CubDebugExit(cudaBindTexture(&offset, ref, d_in, tex_desc, bytes));
}
}
/**
* Unbind textures
*/
static void UnbindTexture()
{
CubDebugExit(cudaUnbindTexture(ref));
}
/**
* Load
*/
static __device__ __forceinline__ Value Load(int offset)
{
Value output;
reinterpret_cast<typename TexVector<Value>::CastType &>(output) = tex1Dfetch(TexVector<Value>::ref, offset);
return output;
}
};
// Texture reference definitions
template <typename Value>
typename TexVector<Value>::TexRef TexVector<Value>::ref = 0;
/******************************************************************************
xgboost/cub/experimental/defunct/example_coo_spmv.cu view on Meta::CPAN
// Create SOA version of coo_graph on host
int num_edges = coo_graph.coo_tuples.size();
VertexId *h_rows = new VertexId[num_edges];
VertexId *h_columns = new VertexId[num_edges];
Value *h_values = new Value[num_edges];
for (int i = 0; i < num_edges; i++)
{
h_rows[i] = coo_graph.coo_tuples[i].row;
h_columns[i] = coo_graph.coo_tuples[i].col;
h_values[i] = coo_graph.coo_tuples[i].val;
}
// Get CUDA properties
Device device_props;
CubDebugExit(device_props.Init());
// Determine launch configuration from kernel properties
int coo_sm_occupancy;
CubDebugExit(device_props.MaxSmOccupancy(
coo_sm_occupancy,
CooKernel<COO_BLOCK_THREADS, COO_ITEMS_PER_THREAD, VertexId, Value>,
COO_BLOCK_THREADS));
int max_coo_grid_size = device_props.sm_count * coo_sm_occupancy * COO_SUBSCRIPTION_FACTOR;
// Construct an even-share work distribution
GridEvenShare<int> even_share(num_edges, max_coo_grid_size, COO_TILE_SIZE);
int coo_grid_size = even_share.grid_size;
int num_partials = coo_grid_size * 2;
// Allocate COO device arrays
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_rows, sizeof(VertexId) * num_edges));
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_columns, sizeof(VertexId) * num_edges));
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_values, sizeof(Value) * num_edges));
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_vector, sizeof(Value) * coo_graph.col_dim));
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_result, sizeof(Value) * coo_graph.row_dim));
CubDebugExit(g_allocator.DeviceAllocate((void**)&d_block_partials, sizeof(PartialProduct) * num_partials));
// Copy host arrays to device
CubDebugExit(cudaMemcpy(d_rows, h_rows, sizeof(VertexId) * num_edges, cudaMemcpyHostToDevice));
CubDebugExit(cudaMemcpy(d_columns, h_columns, sizeof(VertexId) * num_edges, cudaMemcpyHostToDevice));
CubDebugExit(cudaMemcpy(d_values, h_values, sizeof(Value) * num_edges, cudaMemcpyHostToDevice));
CubDebugExit(cudaMemcpy(d_vector, h_vector, sizeof(Value) * coo_graph.col_dim, cudaMemcpyHostToDevice));
// Bind textures
TexVector<Value>::BindTexture(d_vector, coo_graph.col_dim);
// Print debug info
printf("CooKernel<%d, %d><<<%d, %d>>>(...), Max SM occupancy: %d\n",
COO_BLOCK_THREADS, COO_ITEMS_PER_THREAD, coo_grid_size, COO_BLOCK_THREADS, coo_sm_occupancy);
if (coo_grid_size > 1)
{
printf("CooFinalizeKernel<<<1, %d>>>(...)\n", FINALIZE_BLOCK_THREADS);
}
fflush(stdout);
CubDebugExit(cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeEightByte));
// Run kernel (always run one iteration without timing)
GpuTimer gpu_timer;
float elapsed_millis = 0.0;
for (int i = 0; i <= g_timing_iterations; i++)
{
gpu_timer.Start();
// Initialize output
CubDebugExit(cudaMemset(d_result, 0, coo_graph.row_dim * sizeof(Value)));
// Run the COO kernel
CooKernel<COO_BLOCK_THREADS, COO_ITEMS_PER_THREAD><<<coo_grid_size, COO_BLOCK_THREADS>>>(
even_share,
d_block_partials,
d_rows,
d_columns,
d_values,
d_vector,
d_result);
if (coo_grid_size > 1)
{
// Run the COO finalize kernel
CooFinalizeKernel<FINALIZE_BLOCK_THREADS, FINALIZE_ITEMS_PER_THREAD><<<1, FINALIZE_BLOCK_THREADS>>>(
d_block_partials,
num_partials,
d_result);
}
gpu_timer.Stop();
if (i > 0)
elapsed_millis += gpu_timer.ElapsedMillis();
}
// Force any kernel stdio to screen
CubDebugExit(cudaThreadSynchronize());
fflush(stdout);
// Display timing
if (g_timing_iterations > 0)
{
float avg_elapsed = elapsed_millis / g_timing_iterations;
int total_bytes = ((sizeof(VertexId) + sizeof(VertexId)) * 2 * num_edges) + (sizeof(Value) * coo_graph.row_dim);
printf("%d iterations, average elapsed (%.3f ms), utilized bandwidth (%.3f GB/s), GFLOPS(%.3f)\n",
g_timing_iterations,
avg_elapsed,
total_bytes / avg_elapsed / 1000.0 / 1000.0,
num_edges * 2 / avg_elapsed / 1000.0 / 1000.0);
}
// Check results
int compare = CompareDeviceResults(h_reference, d_result, coo_graph.row_dim, true, g_verbose);
printf("%s\n", compare ? "FAIL" : "PASS");
AssertEquals(0, compare);
// Cleanup
TexVector<Value>::UnbindTexture();
CubDebugExit(g_allocator.DeviceFree(d_block_partials));
CubDebugExit(g_allocator.DeviceFree(d_rows));
CubDebugExit(g_allocator.DeviceFree(d_columns));
CubDebugExit(g_allocator.DeviceFree(d_values));
CubDebugExit(g_allocator.DeviceFree(d_vector));
CubDebugExit(g_allocator.DeviceFree(d_result));
delete[] h_rows;
delete[] h_columns;
delete[] h_values;
}
/**
* Compute reference answer on CPU
*/
template <typename VertexId, typename Value>
void ComputeReference(
CooGraph<VertexId, Value>& coo_graph,
Value* h_vector,
Value* h_reference)
{
for (VertexId i = 0; i < coo_graph.row_dim; i++)
{
h_reference[i] = 0.0;
}
for (VertexId i = 0; i < coo_graph.coo_tuples.size(); i++)
{
h_reference[coo_graph.coo_tuples[i].row] +=
coo_graph.coo_tuples[i].val *
h_vector[coo_graph.coo_tuples[i].col];
}
}
/**
* Assign arbitrary values to vector items
*/
template <typename Value>
void AssignVectorValues(Value *vector, int col_dim)
{
for (int i = 0; i < col_dim; i++)
{
vector[i] = 1.0;
}
}
/**
* Main
*/
int main(int argc, char** argv)
{
// Initialize command line
CommandLineArgs args(argc, argv);
g_verbose = args.CheckCmdLineFlag("v");
args.GetCmdLineArgument("i", g_timing_iterations);
// Print usage
if (args.CheckCmdLineFlag("help"))
{
printf("%s\n [--device=<device-id>] [--v] [--iterations=<test iterations>] [--grid-size=<grid-size>]\n"
"\t--type=wheel --spokes=<spokes>\n"
"\t--type=grid2d --width=<width> [--no-self-loops]\n"
"\t--type=grid3d --width=<width> [--no-self-loops]\n"
"\t--type=market --file=<file>\n"
"\n", argv[0]);
exit(0);
}
// Initialize device
CubDebugExit(args.DeviceInit());
// Get graph type
string type;
args.GetCmdLineArgument("type", type);
// Generate graph structure
CpuTimer timer;
timer.Start();
CooGraph<VertexId, Value> coo_graph;
if (type == string("grid2d"))
{
VertexId width;
args.GetCmdLineArgument("width", width);
bool self_loops = !args.CheckCmdLineFlag("no-self-loops");
printf("Generating %s grid2d width(%d)... ", (self_loops) ? "5-pt" : "4-pt", width); fflush(stdout);
if (coo_graph.InitGrid2d(width, self_loops)) exit(1);
} else if (type == string("grid3d"))
{
VertexId width;
args.GetCmdLineArgument("width", width);
bool self_loops = !args.CheckCmdLineFlag("no-self-loops");
printf("Generating %s grid3d width(%d)... ", (self_loops) ? "7-pt" : "6-pt", width); fflush(stdout);
if (coo_graph.InitGrid3d(width, self_loops)) exit(1);
}
else if (type == string("wheel"))
{
VertexId spokes;
args.GetCmdLineArgument("spokes", spokes);
printf("Generating wheel spokes(%d)... ", spokes); fflush(stdout);
if (coo_graph.InitWheel(spokes)) exit(1);
}
else if (type == string("market"))
{
string filename;
args.GetCmdLineArgument("file", filename);
printf("Generating MARKET for %s... ", filename.c_str()); fflush(stdout);
if (coo_graph.InitMarket(filename)) exit(1);
}
else
{
printf("Unsupported graph type\n");
exit(1);
}
timer.Stop();
printf("Done (%.3fs). %d non-zeros, %d rows, %d columns\n",
timer.ElapsedMillis() / 1000.0,
coo_graph.coo_tuples.size(),
coo_graph.row_dim,
coo_graph.col_dim);
( run in 0.764 second using v1.01-cache-2.11-cpan-71847e10f99 )