Skip to content

Instantly share code, notes, and snippets.

@4OH4
4OH4 / median_func.cc
Created November 16, 2021 21:22
Inline configuration parameters
#define IMW __IMW_keyword__ // image width (px)
#define IMH __IMH_keyword__ // image height (px)
#define BX __BX_keyword__ // block index x
#define BY __BY_keyword__ // block index y
#define nTPB (BX*BY) // threads per block
#define IMN 21 // buffer length
@4OH4
4OH4 / check_pycuda.py
Created November 16, 2021 21:14
Checking if PyCUDA is available
# Check if PyCuda is installed
package_name = 'pycuda'
use_cuda = importlib.util.find_spec(package_name) is not None
if use_cuda:
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
@4OH4
4OH4 / pycuda_exampple_pt3.py
Last active November 13, 2021 20:50
Executing a CUDA function with PyCUDA (pt. 3)
# run multiply function on GPU
multiply_func(
result_gpu, a_gpu, b_gpu,
block=(100, 1, 1),
grid=(1, 1, 1)
)
# Get data back from GPU
cuda.memcpy_dtoh(result, result_gpu)
@4OH4
4OH4 / pycuda_exampple_pt2.py
Created November 13, 2021 20:39
Allocating memory on the GPU and copying data with PyCUDA (pt. 2)
# create Python variables
a = np.random.randn(100).astype(np.float32)
b = np.random.randn(100).astype(np.float32)
result = np.random.randn(100).astype(np.float32)
# allocate memory on GPU
a_gpu = cuda.mem_alloc(a.nbytes)
b_gpu = cuda.mem_alloc(b.nbytes)
result_gpu = cuda.mem_alloc(b.nbytes)
@4OH4
4OH4 / pycuda_exampple_pt1.py
Created November 13, 2021 20:29
Creating a CUDA function with PyCUDA (pt. 1)
import numpy as np
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
# Define our function using CUDA code
cuda_func_def = """
__global__ void multiply(float *result, float *a, float *b)
{
const int i = threadIdx.x;
@4OH4
4OH4 / hello.py
Last active January 24, 2021 14:50
FastAPI endpoint that runs a calculation and returns result and metadata
from fastapi import APIRouter
from service.core.models.output import MessageOutput
from service.core.models.input import MessageInput
from service.core.logic.business_logic import run_prime_factor_calculation
router = APIRouter()
@router.post("/hello", response_model=MessageOutput, tags=["hello post"])
def hello_endpoint(inputs: MessageInput):
# Respond to requests on the hello endpoint
@4OH4
4OH4 / output.py
Created January 24, 2021 14:40
Output class that defines the fields and data types of the API response
from pydantic import BaseModel, Field
class MessageOutput(BaseModel):
message1: str = Field(..., title="Greeting")
message2: str = Field(..., title="Calculation result")
n: int = Field(..., title="n: a large integer")
largest_prime_factor: int = Field(..., title="Largest prime factor of n")
elapsed_time: float = Field(..., title="Calculation time (seconds)")
@4OH4
4OH4 / pytesting_hypothesis3.py
Created May 11, 2020 15:41
Using Hypothesis to generate date objects and strings
import datetime
from hypothesis.strategies import dates
from hypothesis import given
from truth.truth import AssertThat
# Module under test
from app.core.worker import Worker
# Generate dates within the four digit year range
@4OH4
4OH4 / pytesting_hypothesis2.py
Last active May 11, 2020 15:32
Hypothesis text generation - example output
# Show some generated text examples
from hypothesis.strategies import text
for _ in range(10):
text().example()
# ''
# '\x17\x14'
# '\x1d\x08'
# '(\U000adacd\x0e\x02\x1e'
@4OH4
4OH4 / tfidf_adv.py
Last active April 13, 2023 21:56
TF-idf model with stopwords and lemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.corpus import stopwords
# Download stopwords list
nltk.download('punkt')
stop_words = set(stopwords.words('english'))