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/tensor.h
ViewVC logotype

View of /branches/cuda/src/lib/include/diderot/tensor.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5628 - (download) (as text) (annotate)
Tue Sep 7 16:23:43 2021 UTC (3 months ago) by adrianlehmann
File size: 2146 byte(s)
Fix various copy semantics
/*! \file tensor.h
 *
 * \author John Reppy
 *
 * Base classes for the generated tensor_shape and tensor_ref_shape classes generated
 * by the compiler.
 */

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

#ifndef _DIDEROT_TENSOR_HXX_
#define _DIDEROT_TENSOR_HXX_

namespace diderot {

template <typename REAL, const int N>
struct tensor_ref {
    const REAL *_data;
    HD
    tensor_ref () : _data(nullptr) { }
    HD
    tensor_ref (const REAL *src) : _data(src) { }
    HD
    REAL const &operator[] (uint32_t i) const
    {
        return this->_data[i];
    }
    HD
    const REAL *base ()
    {
        return this->_data;
    }
    HD
    const REAL *addr (uint32_t i = 0) const
    {
        return &this->_data[i];
    }
};

template <typename REAL, const int N>
struct tensor {
    REAL _data[N];
    HD
    tensor () { }
    HD
    tensor(const tensor &t) {
        this->copy(t.base());
    }
    HD
    tensor (std::initializer_list< REAL > const &il)
    {
        this->copy(il);
    }
    HD tensor (const REAL *src)
    {
        this->copy(src);
    }
    HD
    ~tensor () = default;

    HD void copy (const REAL *src)
    {
#ifdef DIDEROT_TARGET_CUDA
        for (int i=0; i< N; i++) {
            this->base()[i] = src[i];
        }
#else
        std::memcpy(this->_data, src, N*sizeof(REAL));
#endif // DIDEROT_TARGET_CUDA
    }
    HD
    void copy (std::initializer_list< REAL > const &il)
    {
        int32_t i = 0;
        for (auto it = il.begin(); it != il.end(); ++i, ++it) {
            this->_data[i] = *it;
        }
    }
    HD REAL &operator[] (uint32_t i)
    {
        return this->_data[i];
    }
    HD REAL const &operator[] (uint32_t i) const
    {
        return this->_data[i];
    }
    HD REAL *base ()
    {
        return &this->_data[0];
    }
    HD const REAL *base () const
    {
        return &this->_data[0];
    }
    HD const REAL *addr (uint32_t i = 0) const
    {
        return &this->_data[i];
    }
};

} // namespace diderot

#endif // !_DIDEROT_TENSOR_HXX_

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