Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/cuda/src/lib/include/diderot/util.cuh
ViewVC logotype

View of /branches/cuda/src/lib/include/diderot/util.cuh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5628 - (download) (annotate)
Tue Sep 7 16:23:43 2021 UTC (2 months, 2 weeks ago) by adrianlehmann
File size: 2182 byte(s)
Fix various copy semantics
/*! \file util.h
 *
 * \author John Reppy
 *
 * Various utility definitions.
 */

/*
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2017 The University of Chicago
 * All rights reserved.
 */
#ifndef _DIDEROT_UTIL_HXX_
#define _DIDEROT_UTIL_HXX_

#include "base.cuh"

namespace diderot {

// similar to the std::array type, but stripped down
template<typename T, size_t SZ>
struct array {
    typedef T value_type;
    typedef value_type &reference;
    typedef const value_type &const_reference;
    HD
    array() = default;

    template <typename... Args>
    HD
    array(Args&&... values) : _elems{values...} {}

    HD
    array(array *copy) {
        std::copy(std::begin(copy->_elems), std::end(copy->_elems), std::begin(_elems));
    }

    value_type _elems[SZ];

    HD
    reference operator[](size_t i) noexcept { return _elems[i]; }

    HD
    const_reference operator[](size_t i) const noexcept { return _elems[i]; }

    HD
    value_type *data() noexcept { return _elems; }

    HD
    const value_type *data() const noexcept { return _elems; }

    HD
    size_t size() const noexcept { return SZ; }

    cudaError_t copy_to_device_async(array<T, SZ> *tgt) {
        return cudaMemcpyAsync(tgt->_elems, this->_elems, sizeof(T) * SZ, cudaMemcpyHostToDevice);
    }

    cudaError_t copy_to_device(array<T, SZ> *tgt) {
        return cudaMemcpy(tgt->_elems, this->_elems, sizeof(T) * SZ, cudaMemcpyHostToDevice);
    }
};

HD
inline float sign(float x) { return (x > 0) - (x < 0); }

HD
inline double sign(double x) { return (x > 0) - (x < 0); }

namespace __details {

//! set the properties of an output stream (precision and alphabool)
inline void config_ostream(std::ostream &os) {
    os << std::boolalpha;
#ifdef DIDEROT_SINGLE_PRECISION
    // based on https://en.wikipedia.org/wiki/Single-precision_floating-point_format
    os << std::setprecision(9);
#else /* DIDEROT_DOUBLE_PRECISION */
    // based on https://en.wikipedia.org/wiki/Double-precision_floating-point_format
    os << std::setprecision(17);
#endif
}

} // namespace __details

} // namespace diderot

#endif // !_DIDEROT_UTIL_HXX_

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0