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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5628 - (view) (download)

1 : jhr 5597 /*! \file util.h
2 :     *
3 :     * \author John Reppy
4 :     *
5 :     * Various utility definitions.
6 :     */
7 :    
8 :     /*
9 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
10 :     *
11 :     * COPYRIGHT (c) 2017 The University of Chicago
12 :     * All rights reserved.
13 :     */
14 :     #ifndef _DIDEROT_UTIL_HXX_
15 :     #define _DIDEROT_UTIL_HXX_
16 :    
17 : adrianlehm 5628 #include "base.cuh"
18 : adrianlehm 5626
19 : jhr 5597 namespace diderot {
20 :    
21 :     // similar to the std::array type, but stripped down
22 : adrianlehm 5626 template<typename T, size_t SZ>
23 : jhr 5597 struct array {
24 :     typedef T value_type;
25 :     typedef value_type &reference;
26 :     typedef const value_type &const_reference;
27 :     HD
28 : adrianlehm 5626 array() = default;
29 :    
30 :     template <typename... Args>
31 : jhr 5597 HD
32 : adrianlehm 5626 array(Args&&... values) : _elems{values...} {}
33 :    
34 : jhr 5597 HD
35 : adrianlehm 5626 array(array *copy) {
36 :     std::copy(std::begin(copy->_elems), std::end(copy->_elems), std::begin(_elems));
37 :     }
38 :    
39 :     value_type _elems[SZ];
40 :    
41 : jhr 5597 HD
42 : adrianlehm 5626 reference operator[](size_t i) noexcept { return _elems[i]; }
43 :    
44 : jhr 5597 HD
45 : adrianlehm 5626 const_reference operator[](size_t i) const noexcept { return _elems[i]; }
46 : jhr 5597
47 : adrianlehm 5626 HD
48 :     value_type *data() noexcept { return _elems; }
49 :    
50 :     HD
51 :     const value_type *data() const noexcept { return _elems; }
52 :    
53 :     HD
54 :     size_t size() const noexcept { return SZ; }
55 :    
56 : adrianlehm 5622 cudaError_t copy_to_device_async(array<T, SZ> *tgt) {
57 :     return cudaMemcpyAsync(tgt->_elems, this->_elems, sizeof(T) * SZ, cudaMemcpyHostToDevice);
58 :     }
59 :    
60 :     cudaError_t copy_to_device(array<T, SZ> *tgt) {
61 :     return cudaMemcpy(tgt->_elems, this->_elems, sizeof(T) * SZ, cudaMemcpyHostToDevice);
62 :     }
63 : jhr 5597 };
64 : adrianlehm 5626
65 : jhr 5597 HD
66 : adrianlehm 5626 inline float sign(float x) { return (x > 0) - (x < 0); }
67 :    
68 : jhr 5597 HD
69 : adrianlehm 5626 inline double sign(double x) { return (x > 0) - (x < 0); }
70 : jhr 5597
71 :     namespace __details {
72 :    
73 :     //! set the properties of an output stream (precision and alphabool)
74 : adrianlehm 5626 inline void config_ostream(std::ostream &os) {
75 : jhr 5597 os << std::boolalpha;
76 :     #ifdef DIDEROT_SINGLE_PRECISION
77 :     // based on https://en.wikipedia.org/wiki/Single-precision_floating-point_format
78 : adrianlehm 5626 os << std::setprecision(9);
79 : jhr 5597 #else /* DIDEROT_DOUBLE_PRECISION */
80 :     // based on https://en.wikipedia.org/wiki/Double-precision_floating-point_format
81 :     os << std::setprecision(17);
82 :     #endif
83 :     }
84 :    
85 :     } // namespace __details
86 :    
87 :     } // namespace diderot
88 :    
89 :     #endif // !_DIDEROT_UTIL_HXX_

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