Skip to content

Instantly share code, notes, and snippets.

@supplient
supplient / taichi_torch.py
Created November 17, 2022 00:05
Test for taichi + pytorch to use vector operations in kernel function without deepcopy.
import torch
import taichi as ti
ti.init(arch=ti.cuda)
@ti.func
def test_dev(x: ti.types.vector(2, ti.f32)):
return 2*x
@ti.kernel
def test_kernel(x: ti.types.ndarray()):
@supplient
supplient / rightflood.py
Created November 14, 2022 07:30
“向右洪溢”的numba + pytorch实现,封装为了一个pytorch Function,从而继承进了pytorch的自动求导系统中。
import numpy as np
import torch
import numba.cuda as cu
@cu.jit
def enter(upside, down_value, down_index, dumb_R):
di = cu.grid(1)
if di >= down_value.shape[0]:
return
ui = di * 2
@supplient
supplient / inc.py
Last active November 13, 2022 03:50
一个简单的使用numba为pytorch编写使用CUDA kernel的自定义Function的例子。
import torch
import numba.cuda as cu
# Convenient function for specifying CUDA thread block size
_block_size = 512
def _cal_block_num(n):
return int((n-1)/_block_size) + 1
class IncFunction(torch.autograd.Function):
@supplient
supplient / local_sum.py
Created November 10, 2022 10:56
并行计算单调递增的正整数数组的元素的出现次数
import numpy as np
import torch
import numba.cuda as cu
@cu.jit
def enter(upside, downside):
di = cu.grid(1)
if di >= downside.shape[0]:
return
ai = di * 2
@supplient
supplient / filepath2modname.py
Created September 30, 2022 07:34
Get an imported module's qualified name by its file path.
_path2modname = {}
def Filepath2ModuleName(filepath):
''' Get an imported module's qualified name by its file path.
Return `None` if the module is not found or is not imported.
## Usage
``` python
from pkg import mod
@supplient
supplient / sgr.py
Last active September 27, 2022 11:13
A cheetsheet for ascii escape characters which can control terminal text's appearence. (docstring is for vscode's intellisense, which should be parsed as markdown)
'''
# SGR: Select Graphic Rendition
This module is a cheetsheet for ANSI escape characters which can control terminal text's appearence.
## usage
``` python
from sgr import sgr
print(f"{sgr.red + sgr.bg_yellow + sgr.bold}test{sgr.reset} test")
```
@supplient
supplient / pkg_iter_import.py
Created September 18, 2022 08:42
Python snippet for importing all the modules iterativley under a package.
# imports only for type hinting
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from importlib.resources import Package
from types import ModuleType
def ImportAllModules(pkg: Package)-> list[ModuleType]:
'''Import all modules in `pkg`.
@supplient
supplient / gh-pandoc-LICENSE.md
Created June 8, 2022 03:11 — forked from forivall/gh-pandoc-LICENSE.md
Github-style css for pandoc

The MIT License (MIT)

Copyright (c) 2016-2017 Emily M Klassen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

@supplient
supplient / simple_cloth.py
Last active December 2, 2021 13:19
一个基于显式欧拉法的简单布料模拟,基于mass-spring模型,包含张力、平面内剪切力、平面外弯曲力、风力
import taichi as ti
import numpy as np
from random import random
from tqdm import tqdm
from math import sin, pi, sqrt
from timer import Timer
timer = Timer()
################
@supplient
supplient / offset2index.cpp
Created November 6, 2021 08:55
并行算法:反向scan,将片段偏移量转化为片段索引
#include <Windows.h>
// cuda_helpers.h is my own repo
// refer to https://github.com/supplient/CudaHelper
#define CUDA_HELPER_NAMESPACE cuh
#include <cuda_helpers.h>
#include <thrust/scan.h>
#include <thrust/execution_policy.h>
#include <thrust/fill.h>