Skip to content

Commit

Permalink
Merge pull request #1 from william76/outputfile-wcm
Browse files Browse the repository at this point in the history
Add ostream support to print_1Dview
  • Loading branch information
rohit-mp committed Jul 4, 2019
2 parents 9c0de90 + 6a0ebf3 commit 37782f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
9 changes: 5 additions & 4 deletions perf_test/graph/KokkosGraph_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ void print_options(std::ostream &os, const char *app_name, unsigned int indent =
<< spaces << " COLORING_VBDBIT - Use the vertex-based deterministic with bit vectors method." << std::endl
<< std::endl
<< spaces << " Optional Parameters:" << std::endl
<< spaces << " --repeat <N> Set number of test repetitions (Default: 1) " << std::endl
<< spaces << " --verbose Enable verbose mode (record and print timing + extra information)" << std::endl
<< spaces << " --chunksize <N> Set the chunk size." << std::endl
<< spaces << " --dynamic Use dynamic scheduling." << std::endl
<< spaces << " --outputfile <FILE> Output the colors of the nodes to the file." << std::endl
<< spaces << " --repeat <N> Set number of test repetitions (Default: 1) " << std::endl
<< spaces << " --teamsize <N> Set the team size." << std::endl
<< spaces << " --vectorsize <N> Set the vector size." << std::endl
<< spaces << " --verbose Enable verbose mode (record and print timing + extra information)" << std::endl
<< spaces << " --help Print out command line help." << std::endl
<< spaces << " --outputfile <FILE> Output the colors of the nodes to the file." << std::endl
<< spaces << " " << std::endl;
}

Expand Down Expand Up @@ -304,7 +304,8 @@ void run_experiment(
std::cout << "\t"; KokkosKernels::Impl::print_1Dview(kh.get_graph_coloring_handle()->get_vertex_colors());

if( params.coloring_output_file != NULL ) {
KokkosKernels::Impl::print_to_file(kh.get_graph_coloring_handle()->get_vertex_colors(), params.coloring_output_file);
std::ofstream os(params.coloring_output_file, std::ofstream::out);
KokkosKernels::Impl::print_1Dview(os, kh.get_graph_coloring_handle()->get_vertex_colors(), true, "\n");
}
}
}
Expand Down
81 changes: 43 additions & 38 deletions src/common/KokkosKernels_PrintUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "Kokkos_Core.hpp"
#include "Kokkos_Atomic.hpp"
#include "impl/Kokkos_Timer.hpp"
#include <fstream>
#include <ostream>

namespace KokkosKernels{

Expand Down Expand Up @@ -90,69 +90,74 @@ inline void kk_get_histogram(
MyExecSpace().fence();
}


/**
* \brief Prints the given 1D view.
* \param os: Stream to print to. To print to stdout use std::cout, stderr, std::cerr, or a file use an ofstream object.
* \param view: input view to print.
* \param print_all: whether to print all elements or not. If it is false,
* only first and last 10 elements are printed.
* \param print_all: whether to print all elements or not. If it is false, print print_size/2 first and last elements.
* \param sep: Element separator. Default is a single space: " "
* \param print_size: Total elements to print if print_all is false print_size/2 first and last elements are pritned.
* This parameter is not used if print_all is set to true.
*/
template <typename idx_array_type>
inline void kk_print_1Dview(idx_array_type view, bool print_all = false, size_t print_size = 40){

inline void kk_print_1Dview(std::ostream& os, idx_array_type view, bool print_all=false, const char* sep=" ", size_t print_size=40)
{
typedef typename idx_array_type::HostMirror host_type;
typedef typename idx_array_type::size_type idx;
host_type host_view = Kokkos::create_mirror_view (view);
Kokkos::deep_copy (host_view , view);
Kokkos::deep_copy (host_view, view);
idx nr = host_view.extent(0);
if (!print_all){


if (nr > print_size){
if (!print_all)
{
if (nr > print_size)
{
idx n = print_size / 2;
for (idx i = 0; i < n; ++i){
std::cout << host_view(i) << " ";
for (idx i = 0; i < n; ++i)
{
os << host_view(i) << sep;
}
std::cout << "... ... ... ";
os << "... ... ..." << sep;

for (idx i = nr-n; i < nr; ++i){
std::cout << host_view(i) << " ";
for (idx i = nr-n; i < nr; ++i)
{
os << host_view(i) << sep;
}
std::cout << std::endl;
os << std::endl;
}
else {
for (idx i = 0; i < nr; ++i){
std::cout << host_view(i) << " ";
else
{
for (idx i = 0; i < nr; ++i)
{
os << host_view(i) << sep;
}
std::cout << std::endl;
os << std::endl;
}
}
else {
for (idx i = 0; i < nr; ++i){
std::cout << host_view(i) << " ";
else
{
for (idx i = 0; i < nr; ++i)
{
os << host_view(i) << sep;
}
std::cout << std::endl;
os << std::endl;
}
}


/**
* \brief Stores the given view to a file.
* \param view: input view to store.
* \param file: the output file where the view is to be stored.
* \brief Prints the given 1D view.
* \param view: input view to print.
* \param print_all: whether to print all elements or not. If it is false,
* only first and last 20 elements are printed.
*
* This interface is provided for backwards compatiblity.
*/
template <typename idx_array_type>
inline void kk_print_to_file(idx_array_type view, char* file){
inline void kk_print_1Dview(idx_array_type view, bool print_all = false, size_t print_size = 40){

typedef typename idx_array_type::HostMirror host_type;
typedef typename idx_array_type::size_type idx;
host_type host_view = Kokkos::create_mirror_view (view);
Kokkos::deep_copy (host_view , view);
idx nr = host_view.extent(0);
std::ofstream out;
out.open(file);
kk_print_1Dview(std::cout, view, print_all, " ", print_size);

for(idx i = 0; i < nr; ++i){
out << host_view(i) << "\n";
}
}

}
Expand Down
9 changes: 5 additions & 4 deletions src/common/KokkosKernels_Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,15 @@ struct FillSymmetricEdgeList_HashMap{

}
};

template <typename idx_array_type>
void print_1Dview(idx_array_type view, bool print_all = false){
kk_print_1Dview(view, print_all);
void print_1Dview(std::ostream& os, idx_array_type view, bool print_all=false, const char* sep=" "){
kk_print_1Dview(os, view, print_all, sep);
}

template <typename idx_array_type>
void print_to_file(idx_array_type view, char* file){
kk_print_to_file(view, file);
void print_1Dview(idx_array_type view, bool print_all = false){
kk_print_1Dview(view, print_all);
}

template <typename lno_t, typename memory_space>
Expand Down

0 comments on commit 37782f4

Please sign in to comment.