CUDA Video memory operation: CUDA supported c++11__c++

Source: Internet
Author: User
Tags win32 nvcc

compiler and language improvements for CUDA9

Increased support for C + + 14 with the Cuda 9,NVCC compiler, including new features

A generic lambda expression that uses the Auto keyword instead of the parameter type;

Auto lambda = [] (auto A,auto b) {return a * b;};

The return type of the feature is deducted (using the Auto keyword as the return type, as shown in the previous example)

The CONSTEXPR function can contain fewer restrictions, including variable declarations, if,switch, and loops.

The NVCC in CUDA 9 was also faster, compared with CUDA 8, the average compilation time was reduced by 20%, to 50%.

• Expand development platforms and host compilers, including Microsoft Visual Studio 2017, clang 3.9, PGI17.1, and gcc6.x

Cuder: Cuda class with C++11 package

Previously write Cuda: initialization of the environment, application memory, initialization video memory, launch kernel, copy data, release memory. A page is a lot of these complex but necessary operations, and sometimes forget to release some memory.

Today we encapsulate these cuda operations with c++11, and then we can focus on writing kernel code. The CU file is as concise as the GLSL shader file.

For example:./kernel.cu file, there is only one fill function to populate array a.

extern "C"  __global__ void Fill (int * A, int cnt) {
    const int gap = blockdim.x*griddim.x;
    for (int id = blockdim.x*blockidx.x + threadidx.x ID < cnt; id = gap)
        A[id] = ID * 2;
};

The following main.cpp demonstrates the use of the Cuder class.

#include "Cuder.h" const int N = 65536;

std::string Get_ptx_path (const char*);  int main () {int a[n];

    for (int i = 0; i < N; ++i) a[i] = i;

    To prevent arbitrary creation of Cucontext, declare the constructor private, for security, disable copy constructors and copy assignment operators Redips::cuder Cuder = Redips::cuder::getinstance ();
    Add and compile a. cu file [equivalent to GLSL shader file], or add a ptx file directly.
    std::string module_file = "KERNEL.CU";
    std::string module_file = Get_ptx_path ("Kernel.cu");
    
    Cuder.addmodule (Module_file);
    An array of size [sizeof (int) *n] is requested on the video memory and named ["A_dev"] for the identity of the array in the subsequent operation;//If the third argument is not NULL, the CPU-&GT;GPU copy of the data is also performed
    
    Cuder.applyarray ("A_dev", sizeof (int) *n, a); Runs the [fill] function specified in the [./kernel.cu] File, the first two parameters set GridSize and blocksize//{"A_dev", and N} is c++11 in Initializer_list,
    
    If the string corresponds to the previously requested memory array name, otherwise it is the variable type cuder.launch (DIM3, 1, 1), DIM3 (256, 1, 1), Module_file, "fill", {"A_dev", N});
    Copy the [A_dev] video memory array back to [a] Cuder.fetcharray ("A_dev", sizeof (int) *n, a);
return 0;
} std::string Get_ptx_path (const char* cufile) {    std::string path = "./ptx/";
#ifdef WIN32 Path = "win32/";
#else path + + "x64/";
#endif #ifdef _DEBUG Path = "debug/";
#else path + + "release/";
#endif return path + Cufile + ". Ptx"; }

Cuder.addmodule (...) The parameter of the function is a. cu file or a. ptx file.

1. If it is a. cu file, the function is responsible for compiling the function into PTX code. And then encapsulated into the cumodule.
2. If it is a. ptx file, the function simply encapsulates ptx into Cumodule. The
second approach is recommended, as Nvidia's Optix do. The advantage is that compiling at compile time is always better than compile-time, and prompts if code compiles incorrectly. This requires two-point configuration:
2.a adds the Cuda compiler to the build dependencies, and the corresponding. cu file is set to compile with the compiler. The
2.b setting generates the. cu file to the ptx file under the specified path, and then specifies the path to the Ptx file in the program.

The following code is attached to the Cuder.h

#pragma once #include <map> #include <string> #include <vector> #include <cuda.h> #include <nv rtc.h> #include <fstream> #include <sstream> #include <iostream> #include <cudaProfiler.h> #  Include <cuda_runtime.h> #include 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.