andreinechaev / nvcc4jupyter Goto Github PK
View Code? Open in Web Editor NEWA plugin for Jupyter Notebook to run CUDA C/C++ code
License: MIT License
A plugin for Jupyter Notebook to run CUDA C/C++ code
License: MIT License
Hello,
Your examples in https://www.geeksforgeeks.org/how-to-run-cuda-c-c-on-jupyter-notebook-in-google-colaboratory/ works OK.
Then I tried:
%%cuda -n drive/MyDrive/cuda1.cu (cuda1.cu includes the code of your of "maximum element from vector ". I mounted my good drive which includes cuda1.cu. But I get
UsageError: %%cuda is a cell magic, but the cell body is empty.
what am I doing wrong ?
Thanks in advance,
!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git
%load_ext nvcc_plugin
is there any lead so that there will no need to run multiple times.
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?
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!
I followed this tutorial to run CUDA code on COLAB:
but when I give this command:
%load_ext nvcc_plugin
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'
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'
The project is really beautiful but it Needs Installation Instructions and More Detailed Usage Directives for new users to be able to use it easily.
Will create a PR around this if I can learn and use nvcc4jupyter.
Are there any docs or example notebooks you could direct users to?
cc: @andreinechaev
Is it possible to include multiple files and then compile and link them ?
Or do we have to put all the code after %%cu in the cell of the notebook ?
!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.
Wanna use C++20 features inside Cuda code
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
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.
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;
}
CUDA Toolkit isn't included. Is it possible to include this?
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
Is it possible to inclue external header files that are written on external systems like VS ?
Or do I put all headers and source code on colab cells?
If including external header file is possible could you tell me how to use it?
does this extension support nvprof? if not I will be very interested in working with you to implement this feature.
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.