- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Performing a reduction on CUDA
A more challenging example is to use CUDA to sum a vector. This is a reducction and requires communicaiton across threads. In the CUDA model, only threads within a block can share state efficiently by using shared memoery as writing to global memory would be disastrously slow. Therefore, we have to do this in stages - if the shared memory size is \(k\) numbers, we will need \(n\) stages to sum \(k^n\) numbers on the GPU.
Coding strategy
- For simplicity, we set up a reduction that only requires 2 stages
- We have an array \(a\) of length \(n\) that we wish to sum
- We consider \(a\) as being made up of a number blocks of size \(n/k\)
- The CPU will launch a kernel to find the \(k\) partial sums of \(a\)
- Each sum will be of consecutive \(n/k\) elements in the original array
- The summation of pairs of numbers is performed by a device-only sub-kernel launched by the GPU
- The summation is done in 3 steps:
- Each thread in a block writes its values to shared memory in the location corresponding to the thread index
- Synchronize threads to make sure that all threads have completed writing before proceeding
- The first thread in the block sums up the values in shared memory (the rest are idle) and stores in the location corresponding to the block index
- Finally, the CPU launches the kernel again to sum the partial sums
- For efficiency, we overwrite partial sums in the original vector
Note that other reductions (e.g. min, max) etc follow the same strategy - just swap the device kernel with another one.
The two strateiges of mapping each operation to a thread and reduction to combine results from several threads are the basic buiding blocks of many CUDA algorithms. It is surprising how many alogrithms can be formulated as combinaitons of mapping and redcution steps - and we will revisit this pattern with Hadoop/SPARK.
@cuda.jit('int32(int32, int32)', device=True) def dev_sum(a, b): return a + b @cuda.jit('void(int32[:], int32[:])') def cu_sum(a, b): "Simple implementation of reduction kernel" # Allocate static shared memory of 512 (max number of threads per block for CC < 3.0) # This limits the maximum block size to 512. sa = cuda.shared.array(shape=(612,), dtype=int32) tx = cuda.threadIdx.x bx = cuda.blockIdx.x bw = cuda.blockDim.x i = tx + bx * bw if i < a.shape[0]: sa[tx] = a[i] if tx == 0: # Uses the first thread of each block to perform the actual # reduction s = sa[tx] cuda.syncthreads() for j in range(1, bw): s = dev_sum(s, sa[j]) b[bx] = s k = 4 # numbers to be added in the partial sum (must be less than or equal to 512) n = 6*4 # total length of vector to be summed a = np.random.randint(0, n, n).astype(np.int32) print 'a =', a print 'a.sum() = ', a.sum() d_a = cuda.to_device(a) d_b = cuda.to_device(b, copy=False) griddim = (k, 1) blockdim = (a.size//k, 1) cu_sum[griddim, blockdim](d_a, d_a) d_a.to_host() print 'a =', a cu_sum[1, griddim](d_a[:k], d_a) d_a.to_host() print 'a =', a
a = [14 20 16 11 12 4 2 3 3 16 8 12 12 19 6 15 20 20 20 21 8 5 11 17] a.sum() = 295 a = [77 44 92 82 12 4 2 3 3 16 8 12 12 19 6 15 20 20 20 21 8 5 11 17] a = [295 44 92 82 12 4 2 3 3 16 8 12 12 19 6 15 20 20 20 21 8 5 11 17]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论