Giter Site home page Giter Site logo

cuda_c's Introduction

CUDA Playground

This repository contains files that primarily focus on running Python code in conjunction with C and CUDA (C-based) code.

Addition

In the Addition section, we explore a simple numerical addition operation. It's noteworthy that for compiling the C code, we use nvcc (NVIDIA CUDA Compiler)

The asynchronous behavior of print statements between Python and C, especially when a C function is called from Python, arises due to differences in how each language handles output buffering:

  • Python's Print Function: Automatically flushes the output buffer each time it's called, typically displaying the output immediately.
  • C's printf Function: May not immediately flush the output buffer, particularly when output is redirected or part of a shared library. This can lead to delayed or batched display of output. When Python calls a C function, these differences can result in out-of-order or seemingly asynchronous output between the two, as the timing of when outputs are actually written to the terminal or console can vary.

Addition_CUDA

In this example, we demonstrate the integration of CUDA with Python for performing an addition operation. The code consists of three main components:

  1. CUDA Kernel (sumKernel):

    • This kernel is written in CUDA C and is responsible for performing the addition operation. Each thread in the kernel adds its index to a shared result variable using atomicAdd to ensure thread safety. A print statement is included in the kernel for the first thread to output the intermediate sum.
  2. C Function (kernel_launcher):

    • Defined with extern "C" to prevent name mangling, this function acts as a bridge between the CUDA kernel and the Python environment. It handles memory allocation on the GPU, launches the CUDA kernel, and then copies the result back to the host memory. After executing the kernel, it prints the final sum from the C perspective.
  3. Python Integration:

    • Using Python's ctypes library, we load the compiled CUDA code as a shared library. The kernel_launcher function is set up with appropriate argument and return types, and is then called from Python. This showcases how Python can leverage CUDA for high-performance computations.

Execution Flow:

  1. The Python script calls kernel_launcher with the number of threads.
  2. kernel_launcher allocates memory, launches the sumKernel, and retrieves the result.
  3. The sumKernel performs the addition in parallel on the GPU.
  4. Outputs from both the CUDA kernel and the C function are printed, followed by the final result printed in Python.

This example highlights the synergy between Python and CUDA, enabling efficient computations with parallel processing capabilities of GPUs.

cuda_c's People

Contributors

rong-tao avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.