一点数学
主定理
形如 $$ T(n) = a\cdot T(\frac{n}{b}) + f(n)$$
若$$f(n) = O(n^{log_b a-\epsilon})$$,则 $$T(n) = \Theta(n^{log_b a}) $$
若$$f(n) = O(n^{log_b a}\cdot log^k n)$$,则$$ T(n) = \Theta(n^{log_b a} log^{k+1} n) $$
形如 $$ T(n) = a\cdot T(\frac{n}{b}) + f(n)$$
若$$f(n) = O(n^{log_b a-\epsilon})$$,则 $$T(n) = \Theta(n^{log_b a}) $$
若$$f(n) = O(n^{log_b a}\cdot log^k n)$$,则$$ T(n) = \Theta(n^{log_b a} log^{k+1} n) $$
map(func, a)
对a中元素应用func,python3返回一个迭代器
对迭代器x通过 next(x)
获取元素list(x)
将剩余元素转为列表dir(x)
获取所有属性与方法id(x)
type(x)
x类型
python -i [a.py]
在interactive shell中运行a.pyadd = lambda x, y : x + y
lambda函数b = filter(lambda x : x > 1, a)
对list a进行filter
for i, num in enumerate(a):
print(i, num)
i为编号,num为a中元素
ord(x)
将字符转为asacii码zip(l1, l2, l3 {...})
依次提取l1, l2, l3 ...中元素,打包为元组
func.__code__.co_argcount
函数func参数数量func.__code__.co_varnames
函数参数列表
发现会日常忘记git的基本操作,开个坑记一下
git add -u
加入所有对已经track的文件的修改
git add -A
加入所有文件,包括新增和删除
git reset --hard HEAD^
回退到head的前一次commit
git reset --soft HEAD^
只撤销commit操作,将add的文件还原到工作区
SST和所有外部组件用相同编译器编译
SM : streaming Multiprocessor
基础模块,有4block ( 16 fp32, 8 fp64, 16 int32, 128K L1 cache, 64k register)
SIMT : single instruction multiple threads
一组32 cores执行相同指令不同数据
gridDim.x
blockIdx.x
blockDim.x
threadIdx.x
nvcc --ptxas-options=-v
: 显示寄存器和 shared mem 信息
cudaHostAlloc
和 cudaFreeHost
分配新内存
使用 cudaHostRegister
将 malloc 的内存变为 page-lockedgolbal
: host调用device执行函数__syncthreads()
同步一个block内的threadshared int a[]
: 每个block一个共享内存中的aT __ldg(const T address)
从address读取一个数据T (从read-only data cache中读取)
cuda 中类似 C++ STL
thrust::device_ptr<int> d_arr = thrust::device_pointer_cast<int>(d_bin_counter);
// host指针转device_ptr
thrust::exclusive_scan(d_arr, d_arr + SEGBIN_NUM, d_arr); // 每个位置变为自己之前不包括自己的前缀和
atomicAdd(int* address, int val)
将 val 原子加到 address
cudaDeviceSynchronize()
block直到所有设备同步
GPU中所有 active thread 被分配了单独的寄存器,当切换线程时不需要交换寄存器
cuda 中所有 kernel launch 都为异步
在开始和结束 CPU timer 之前都需要调用 cudaDeviceSynchronize
cuda 中 default stream (stream 0) 可以保证所有之前任何 stream 的调用均在前执行,且所有之后任何 stream 的调用均在后执行 (serializing)
事件计时:
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord( start, 0 );
kernel<<<grid,threads>>> ( d_odata, d_idata, size_x, size_y,
NUM_REPS);
cudaEventRecord( stop, 0 );
cudaEventSynchronize( stop );
cudaEventElapsedTime( &time, start, stop );
cudaEventDestroy( start );
cudaEventDestroy( stop );
只能在 stream 0 中用此方法计时,其他 stream 无法保证顺序
int deviceCount;
cudaGetDeviceCount(&deviceCount); // device 数量
cudaGetDeviceProperties(&deviceProp, 1); // 获取 device 1 的 properties
cudaSetDevice(0); // 设置当前 device 为 0,之后的内存分配和 kernel launch 等操作均在 device 0 上进行
在 CUDA 中用户可以创建多个 stream,但同时不会有超过 32 个 kernel 执行
在一些 device 上, L1 cache 和 shared memory 共享硬件资源
#pragma unroll [factor]
: 强制循环展开,factor为展开参数,factor=1为不展开restrict
: 指针重名优化(Pointer Aliasing),显示指定没有重名指针
tensor core 理论 315TFLOPS
A100 dram 1500GB/s
A100 L2 40MB 所有SM共享 V100 L2 6MB
compute capability :
A100 : 8.0
V100 : 7.0
P100 : 6.0
额,请在编译时指定正确的 compute capability
hipLaunchKernelGGL(MyKernel, dim3(gridDim), dim3(groupDim), 0/*dynamicShared*/, 0/*stream*/, a, b, c, n /*kernel函数参数*/);
// Replace MyKernel<<<dim3(gridDim), dim3(gridDim), 0, 0>>> (a,b,c,n);