Solving projecteuler with pyCUDA

Project Euler is a great pastime activity, and it just got even more exciting with the use of pyCUDA. PyCUDA lets you access Nvidia‘s CUDA parallel computation API from Python. So I decided to solve a couple of the problems with the use of my graphics card and pyCUDA.

Here is the solution to problem 1:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Find the sum of all the multiples of 3 or 5 below 1000.

import pycuda.gpuarray as gpuarray
import pycuda.autoinit
from pycuda.elementwise import ElementwiseKernel

numbers = gpuarray.arange(0,1000,1,dtype='float32')

# the elementwise kernel can only have a single operation, without a semicolon in the end.
# see http://documen.tician.de/pycuda/array.html#module-pycuda.elementwise
modulo = ElementwiseKernel(
    "float *values,float *results",
    "results[i]=(fmod(values[i], 3) == 0 || fmod(values[i], 5) == 0)?values[i]:0",
    "modulo")

results = gpuarray.empty_like(numbers)
modulo(numbers, results)

print gpuarray.sum(results,dtype='float32')

Of course, this is an extreme overkill, but I had to start somewhere to learn CUDA development. :-)


Written by Jannich Brendle ons 19 januar 2011 In Programming

tags: cudainterestingmathproject eulerpython

Related posts

social