|
| 1 | +// Copyright (c) 1993-2016, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions |
| 5 | +// are met: |
| 6 | +// * Redistributions of source code must retain the above copyright |
| 7 | +// notice, this list of conditions and the following disclaimer. |
| 8 | +// * Redistributions in binary form must reproduce the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer in the |
| 10 | +// documentation and/or other materials provided with the distribution. |
| 11 | +// * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +// contributors may be used to endorse or promote products derived |
| 13 | +// from this software without specific prior written permission. |
| 14 | +// |
| 15 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +#include <cstdio> |
| 27 | +#include <cuda_fp16.h> |
| 28 | +#include <assert.h> |
| 29 | +#include "fp16_conversion.h" |
| 30 | + |
| 31 | +// This is a simple example of using FP16 types and arithmetic on |
| 32 | +// GPUs that support it. The code computes an AXPY (A * X + Y) operation |
| 33 | +// on half-precision (FP16) vectors (HAXPY). |
| 34 | + |
| 35 | +// Convenience function for checking CUDA runtime API results |
| 36 | +// can be wrapped around any runtime API call. No-op in release builds. |
| 37 | +inline |
| 38 | +cudaError_t checkCuda(cudaError_t result) |
| 39 | +{ |
| 40 | +#if defined(DEBUG) || defined(_DEBUG) |
| 41 | + if (result != cudaSuccess) { |
| 42 | + fprintf(stderr, "CUDA Runtime Error: %s\n", cudaGetErrorString(result)); |
| 43 | + assert(result == cudaSuccess); |
| 44 | + } |
| 45 | +#endif |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +__global__ |
| 50 | +void haxpy(int n, half a, const half *x, half *y) |
| 51 | +{ |
| 52 | + int start = threadIdx.x + blockDim.x * blockIdx.x; |
| 53 | + int stride = blockDim.x * gridDim.x; |
| 54 | + |
| 55 | +#if __CUDA_ARCH__ >= 530 |
| 56 | + int n2 = n/2; |
| 57 | + half2 *x2 = (half2*)x, *y2 = (half2*)y; |
| 58 | + |
| 59 | + for (int i = start; i < n2; i+= stride) |
| 60 | + y2[i] = __hfma2(__halves2half2(a, a), x2[i], y2[i]); |
| 61 | + |
| 62 | + // first thread handles singleton for odd arrays |
| 63 | + if (start == 0 && (n%2)) |
| 64 | + y[n-1] = __hfma(a, x[n-1], y[n-1]); |
| 65 | +#else |
| 66 | + for (int i = start; i < n; i+= stride) { |
| 67 | + y[i] = __float2half(__half2float(a) * __half2float(x[i]) |
| 68 | + + __half2float(y[i])); |
| 69 | + } |
| 70 | +#endif |
| 71 | +} |
| 72 | + |
| 73 | +int main(void) { |
| 74 | + const int n = 100; |
| 75 | + |
| 76 | + const half a = approx_float_to_half(2.0f); |
| 77 | + |
| 78 | + half *x, *y; |
| 79 | + checkCuda(cudaMallocManaged(&x, n * sizeof(half))); |
| 80 | + checkCuda(cudaMallocManaged(&y, n * sizeof(half))); |
| 81 | + |
| 82 | + for (int i = 0; i < n; i++) { |
| 83 | + x[i] = approx_float_to_half(1.0f); |
| 84 | + y[i] = approx_float_to_half((float)i); |
| 85 | + } |
| 86 | + |
| 87 | + const int blockDim = 256; |
| 88 | + const int nBlocks = (n + blockDim - 1) / blockDim; |
| 89 | + |
| 90 | + haxpy<<<blockDim, nBlocks>>>(n, a, x, y); |
| 91 | + |
| 92 | + checkCuda(cudaDeviceSynchronize()); |
| 93 | + |
| 94 | + for (int i = 0; i < n; i++) |
| 95 | + printf("%f\n", half_to_float(y[i])); |
| 96 | + |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
0 commit comments