Code Monkey home page Code Monkey logo

nvcc4jupyter's Introduction

nvcc4jupyter: CUDA C++ plugin for Jupyter Notebook

Testing Python Versions CI - Test Coverage
Code Quality Code style: black security: bandit
Package PyPI Latest Release PyPI Downloads

nvcc4jupyter is a Jupyter Notebook plugin that provides cell and line magics to allow running CUDA C++ code from a notebook. This is especially useful when combined with a hosted service such a Google's Colab which provide CUDA capable GPUs and you can start learning CUDA C++ without having to install anything or even to own a GPU yourself.

Table of Contents

Main Features

Here are just a few of the things that nvcc4jupyter does well:

Install

The installer for the latest released version is available at the Python Package Index (PyPI).

pip install nvcc4jupyter

Usage

First, load the extension to enable the magic commands:

%load_ext nvcc4jupyter

Running a quick CUDA Hello World program:

%%cuda
#include <stdio.h>

__global__ void hello(){
    printf("Hello from block: %u, thread: %u\n", blockIdx.x, threadIdx.x);
}

int main(){
    hello<<<2, 2>>>();
    cudaDeviceSynchronize();
}

For more advanced use cases, see the documentation.

Documentation

The official documentation is hosted on readthedocs.

License

MIT

Contributing

The recommended setup for development is using the devcontainer in GitHub Codespaces or locally in VSCode.

If not using the devcontainer you need to install the package with the development dependencies and install the pre-commit hook before commiting any changes:

pip install -e .[dev]
pre-commit install

Go to Top

nvcc4jupyter's People

Contributors

cosminc98 avatar ianneee avatar msaroufim avatar project-tuva avatar ssakuh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nvcc4jupyter's Issues

Problem on %load_ext nvcc_plugin, when running CUDA

I followed this tutorial to run CUDA code on COLAB:

https://medium.com/@iphoenix179/running-cuda-c-c-in-jupyter-or-how-to-run-nvcc-in-google-colab-663d33f53772

but when I give this command:

%load_ext nvcc_plugin

I get this error:
Cloning git://github.com/andreinechaev/nvcc4jupyter.git to /tmp/pip-req-build-qlz3kouf
ERROR: Command errored out with exit status 128: git clone -q git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-qlz3kouf Check the logs for full command output.

ModuleNotFoundError Traceback (most recent call last)
in <cell line: 2>()
1 get_ipython().system('pip install git+git://github.com/andreinechaev/nvcc4jupyter.git')
----> 2 get_ipython().run_line_magic('load_ext', 'nvcc_plugin')

7 frames
in load_ext(self, module_str)

/usr/lib/python3.9/importlib/_bootstrap.py in find_and_load_unlocked(name, import)

ModuleNotFoundError: No module named 'nvcc_plugin'

cuda_runtime.h error on kaggle

I am trying to run a basic CUDA code on kaggle.

%%cuda
#include <stdio.h>
​
__global__ void HelloKernel() {
    printf("\tHello from GPU (device)\n");
}
​
int main() {
  printf("Hello from CPU (host) before kernel execution\n");
  HelloKernel<<<1,32>>>();
  cudaDeviceSynchronize();
  printf("Hello from CPU (host) after kernel execution\n");
  return 0;
}

The same code works fine on Colab but on Kaggle it gives this error.

cc1plus: fatal error: cuda_runtime.h: No such file or directory
compilation terminated.

Any idea on what might be causing this?

nvprof

does this extension support nvprof? if not I will be very interested in working with you to implement this feature.

Cuda not showing output in Google collab

I'm using this code, to prepare the environment

!/usr/local/cuda/bin/nvcc --version
!gcc-5 -v
!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git
%load_ext nvcc_plugin
!rm /usr/bin/gcc
!rm /usr/bin/g++
!ln -s /usr/bin/gcc-5 /usr/bin/gcc
!ln -s /usr/bin/g++-5 /usr/bin/g++

Then when I try to run this simple example:

%%cu
#include <stdio.h>

void helloCPU()
{
  printf("Hello from the CPU.\n");
}

/*
 * The addition of `__global__` signifies that this function
 * should be launced on the GPU.
 */

__global__ void helloGPU()
{
  printf("Hello from the GPU.\n");
}

int main()
{
   helloCPU();


  /*
   * Add an execution configuration with the <<<...>>> syntax
   * will launch this function as a kernel on the GPU.
   */

  helloGPU<<<1, 1>>>();

  /*
   * `cudaDeviceSynchronize` will block the CPU stream until
   * all GPU kernels have completed.
   */

  cudaDeviceSynchronize();
}

It runs but I get the output from the CPU but not the GPU

'Hello from the CPU.\n'

Error while using in Colab

!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

Error

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting git+git://github.com/andreinechaev/nvcc4jupyter.git
Cloning git://github.com/andreinechaev/nvcc4jupyter.git to /tmp/pip-req-build-uc38iq2f
Running command git clone -q git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-uc38iq2f
fatal: unable to connect to github.com:
github.com[0: 140.82.114.4]: errno=Connection timed out

WARNING: Discarding git+git://github.com/andreinechaev/nvcc4jupyter.git. Command errored out with exit status 128: git clone -q git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-uc38iq2f Check the logs for full command output.
ERROR: Command errored out with exit status 128: git clone -q git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-uc38iq2f Check the logs for full command output.

errors: " helper.py TypeError: a bytes-like object is required, not 'str' " and " 'Namespace' object has no attribute 'timeit' "

Hi, nice project. I came to learn a lot from it.

I got these errors when using %%cuda --name sourcecodename.cu --compile true
1)
" helper.py TypeError: a bytes-like object is required, not 'str' "
2)
" 'Namespace' object has no attribute 'timeit' "

I got to solve them as follows:
1)in v2.py, line 49, add:
@argument("-t", "--timeit", action='store_true', help='flag to return timeit result instead of stdout')

2)in v2.py, line change tp:
res.decode("utf8")

I was trying to use the plugin with opencv in Colab.
I think I solved it. Allow me to share the solution here.

modified version of v2.py:

## include opencv
import os
import subprocess

from IPython.core.magic import Magics, cell_magic, magics_class
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
from common import helper

compiler = '/usr/local/cuda/bin/nvcc'

result = subprocess.run(['pkg-config', '--cflags',  '--libs', 'opencv' ], stdout=subprocess.PIPE)
cv_string = result.stdout.decode('utf-8')

@magics_class
class NVCCPluginV2(Magics):

    def __init__(self, shell):
        super(NVCCPluginV2, self).__init__(shell)
        self.argparser = helper.get_argparser()
        current_dir = os.getcwd()
        self.output_dir = os.path.join(current_dir, 'src')
        if not os.path.exists(self.output_dir):
            os.mkdir(self.output_dir)
            print(f'created output directory at {self.output_dir}')
        else:
            print(f'directory {self.output_dir} already exists')

        self.out = os.path.join(current_dir, "result.out")
        print(f'Out bin {self.out}')

    @staticmethod
    def compile(output_dir, file_paths, out):
        res = subprocess.check_output( [compiler, '-I' + output_dir, file_paths, "-o", out, '-Wno-deprecated-gpu-targets',  *( cv_string[:-1].split(" ") ) ], stderr=subprocess.STDOUT)
        print(   [compiler, '-I' + output_dir, file_paths, "-o", out, '-Wno-deprecated-gpu-targets',  *( cv_string[:-1].split(" ") )   ] )
        print(res)
        print( type(res) )
        helper.print_out(res.decode("utf8"))

    def run(self, timeit=False):
        if timeit:
            stmt = f"subprocess.check_output(['{self.out}'], stderr=subprocess.STDOUT)"
            output = self.shell.run_cell_magic(
                magic_name="timeit", line="-q -o import subprocess", cell=stmt)
        else:
            output = subprocess.check_output(
                [self.out], stderr=subprocess.STDOUT)
            output = output.decode('utf8')

        helper.print_out(output)
        return None

    @magic_arguments()
    @argument('-n', '--name', type=str, help='file name that will be produced by the cell. must end with .cu extension')
    @argument('-c', '--compile', type=bool, help='Should be compiled?')
    @argument("-t", "--timeit", action='store_true',  help='flag to return timeit result instead of stdout')
    @cell_magic
    def cuda(self, line='', cell=None):
        args = parse_argstring(self.cuda, line)
        ex = args.name.split('.')[-1]
        if ex not in ['cu', 'h']:
            raise Exception('name must end with .cu or .h')

        if not os.path.exists(self.output_dir):
            print(f'Output directory does not exist, creating')
            try:
                os.mkdir(self.output_dir)
            except OSError:
                print(f"Creation of the directory {self.output_dir} failed")
            else:
                print(f"Successfully created the directory {self.output_dir}")

        file_path = os.path.join(self.output_dir, args.name)
        with open(file_path, "w") as f:
            f.write(cell)

        if args.compile:
            try:
                print("about to compile")
                self.compile(self.output_dir, file_path, self.out)
                print("about to run")
                output = self.run(timeit=args.timeit)
            except subprocess.CalledProcessError as e:
                print(output.decode("utf8"))
                helper.print_out(e. output.decode("utf8") )
                output = None
        else:
            output = f'File written in {file_path}'

        return output

    @cell_magic
    def cuda_run(self, line='', cell=None):
        try:
            args = self.argparser.parse_args(line.split())
        except SystemExit:
            self.argparser.print_help()
            return

        try:
            cuda_src = os.listdir(self.output_dir)
            cuda_src = [os.path.join(self.output_dir, x)
                        for x in cuda_src if x[-3:] == '.cu']
            print(f'found sources: {cuda_src}')
            self.compile(self.output_dir, ' '.join(cuda_src), self.out)
            output = self.run(timeit=args.timeit)
        except subprocess.CalledProcessError as e:
            helper.print_out(e.output.decode("utf8"))
            output = None

        return output

important changes are:

result = subprocess.run(['pkg-config', '--cflags',  '--libs', 'opencv' ], stdout=subprocess.PIPE)
cv_string = result.stdout.decode('utf-8')
. . .
    def compile(output_dir, file_paths, out):
        res = subprocess.check_output( [compiler, '-I' + output_dir, file_paths, "-o", out, '-Wno-deprecated-gpu-targets',  *( cv_string[:-1].split(" ") ) ], stderr=subprocess.STDOUT)

I tested it with the following cell (not a thoroughly test though)

%%cuda --name sourcecodename.cu --compile true

#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    std::cout << cv::getBuildInformation() << std::endl;
    return 0;
}

Include addtional cu/c++ files in jupyter

When I use jupyter I will develop a normal python package and use notebook as an interface to do some experiments upon the released package.

I wonder if I can include addtional customer cu/c++ files in a cell. For example, we could write gtests in jupyter to do experiments upon the cu package and share the result to peers.

Is there any way to do that?

How do you pass image data from cv2 python to a %%cu file

I am trying to use this plugin on Google colab. I load the image using cv2 and now i want to do image processing in CUDA kernel. I am at a fix now in how to access the image data loaded from cv2 python to the CUDA cell (%%cu). Can you share a simple example or point me to the relevant references?

If it is possible to read the image file in cu file that would be awesome. Currently, I tried doing this but it fails.

 FILE* fp = fopen("/content/logo.raw", "rb");
 fread(pData, sizeof(unsigned char), SIZE, fp);
 fclose(fp);

Thanks,
MMMovania

During installing NVCC plugin it's showing Module Not Found.

I am trying to load the nvcc plugin using the %load_ext nvcc_plugin command in the google colab and it gives the following error
logo-OYJ34ERC

Collecting git+git://github.com/andreinechaev/nvcc4jupyter.git
Cloning git://github.com/andreinechaev/nvcc4jupyter.git to /tmp/pip-req-build-ntuq7rz4
Running command git clone --filter=blob:none --quiet git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-ntuq7rz4
fatal: unable to connect to github.com:
github.com[0: 140.82.121.4]: errno=Connection timed out

error: subprocess-exited-with-error

× git clone --filter=blob:none --quiet git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-ntuq7rz4 did not run successfully.
│ exit code: 128
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× git clone --filter=blob:none --quiet git://github.com/andreinechaev/nvcc4jupyter.git /tmp/pip-req-build-ntuq7rz4 did not run successfully.
│ exit code: 128
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
[<ipython-input-3-79d7ff0447b0>](https://localhost:8080/#) in <cell line: 2>()
    1 get_ipython().system('pip install git+git://github.com/andreinechaev/nvcc4jupyter.git')
----> 2 get_ipython().run_line_magic('load_ext', 'nvcc_plugin')

7 frames
<decorator-gen-57> in load_ext(self, module_str)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'nvcc_plugin'

Is there any solution to fix this error?

Just confirming it is not issue with installing the package but with loading the nvcc plugin itself.
image

new line is not detected

edw
\n is not recognized by nvcc4jupyter

it will be better if it give output not in a one line but in sptep wise output

ModuleNotFoundError: No module named 'nvcc_plugin'

Hi!

I am running the he following to load enable colab to run cuda:

!nvcc --version
!pip install git+https://github.com/andreinechaev/nvcc4jupyter.git
%load_ext nvcc_plugin

It was working fine till yesterday and today I come across this issue. I see someone else also facing this issue today.

Error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
[<ipython-input-2-5df49c27db81>](https://localhost:8080/#) in <cell line: 1>()
----> 1 get_ipython().run_line_magic('load_ext', 'nvcc_plugin')

7 frames
<decorator-gen-57> in load_ext(self, module_str)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'nvcc_plugin'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

Has something changed? Thank you!

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.