-
-
Save tjyuyao/68d83fa3b6b92435637a9c5dcb79a027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import pycuda.autoprimaryctx | |
from pycuda.compiler import SourceModule | |
mod = SourceModule(""" | |
__global__ void multiply_them(float *dest, float *a, float *b) | |
{ | |
const int i = threadIdx.x; | |
dest[i] = a[i] * b[i]; | |
} | |
""") | |
multiply_them = mod.get_function("multiply_them") | |
class Holder(pycuda.driver.PointerHolderBase): | |
def __init__(self, t): | |
super(Holder, self).__init__() | |
self.t = t | |
self.gpudata = t.data_ptr() | |
def get_pointer(self): | |
return self.t.data_ptr() | |
a = torch.randn(400, dtype=torch.float32).cuda() | |
b = torch.randn(400, dtype=torch.float32).cuda() | |
dest = torch.empty_like(a) | |
multiply_them( | |
Holder(dest), | |
Holder(a), | |
Holder(b), | |
block=(400,1,1), grid=(1,1)) | |
torch.cuda.synchronize() | |
print(dest-a*b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment