GithubHelp home page GithubHelp logo

zakattacktwitter / cutorch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from torch/cutorch

0.0 1.0 0.0 9.92 MB

A CUDA backend for Torch7

License: Other

CMake 0.66% Lua 19.97% C 34.51% Cuda 44.78% C++ 0.07%

cutorch's Introduction

cutorch

Cutorch provides a CUDA backend for torch7.

Cutorch provides the following:

  • a new tensor type: torch.CudaTensor that acts like torch.FloatTensor, but all it's operations are on the GPU. Most of the tensor operations are supported by cutorch. There are a few missing ones, which are being implemented. The missing list can be found here: torch#70
  • cutorch.* - Functions to set/get GPU, get device properties, memory usage, set/get low-level streams, set/get random number generator's seed, synchronization etc. They are described in more detail below.

torch.CudaTensor

This new tensor type behaves exactly like a torch.FloatTensor, but has a couple of extra functions of note:

  • t:getDevice() - Given a CudaTensor t, you can call :getDevice on it to find out the GPU ID on which the tensor memory is allocated.

###cutorch.* API

  • cutorch.synchronize() : All of the CUDA API is asynchronous (barring a few functions), which means that you can queue up operations. To wait for the operations to finish, you can issue cutorch.synchronize() in your code, when the code waits for all GPU operations on the current GPU to finish.

  • cutorch.setDevice(i) : If one has multiple-GPUs, you can switch the default GPU (to allocate CUDA tensors and do operations). The GPU IDs are 1-indexed, so having 4 GPUs means, you can setDevice(1), setDevice(2), setDevice(3), setDevice(4).

  • idx = cutorch.getDevice() : Returns the currently set GPU device index.

  • count = cutorch.getDeviceCount() : Gets the number of available GPUs.

  • totalMemory, freeMemory = cutorch.getMemoryUsage(devID) : Gets the total and free memory in bytes for the given device ID.

  • cutorch.seed([devID]) - Sets and returns a random seed for the current or specified device.

  • cutorch.seedAll() - Sets and returns a random seed for all available GPU devices.

  • cutorch.initialSeed([devID]) - Returns the seed for the current or specified device

  • cutorch.manualSeed(seed [, device]) - Sets a manually specified RNG seed for the current or specified device

  • cutorch.manualSeedAll(seed) - Sets a manually specified RNG seed for all available GPUs

  • cutorch.getRNGState([device]) - returns the current RNG state in the form of a byte tensor, for the current or specified device.

  • cutorch.setRNGState(state [, device]) - Sets the RNG state from a previously saved state, on the current or specified device.

  • cutorch.getState() - Returns the global state of the cutorch package. This state is not for users, it stores the raw RNG states, cublas handles and other thread and device-specific stuff.

  • cutorch.withDevice(devID, f) - This is a convenience for multi-GPU code, that takes in a device ID as well as a function f. It switches cutorch to the new device, executes the function f, and switches back cutorch to the original device.

Low-level streams functions (dont use this as a user, easy to shoot yourself in the foot):

  • cutorch.reserveStreams(n): creates n user streams for use on every device.
  • n = cutorch.getNumStreams(): returns the number of user streams available on every device. By default, this is 0, meaning only the default stream (stream 0) is available.
  • cutorch.setStream(n): specifies that the current stream active for the current device (or any other device) is n. This is preserved across device switches. 1-N are user streams, 0 is the default stream.
  • n = cutorch.getStream(): returns the current stream active. By default, returns 0.
  • cutorch.setDefaultStream(): an alias for cutorch.setStream(0)
  • cutorch.streamWaitFor(streamWaiting, {streamsToWaitOn...}): A 1-to-N-way barrier. streamWaiting will wait for the list of streams specified to finish executing all kernels/events/barriers. Does not block any of the streamsToWaitOn. Current device only.
  • cutorch.streamWaitForMultiDevice(deviceWaiting, streamWaiting, {[device]={streamsToWaitOn...}...}): (deviceWaiting, streamWaiting) will wait on the list of (device, streams...) pairs; handles single or multiple device. cutorch.streamWaitForMultiDevice, a, b, {[a]={streams...}}) is equivalent to cutorch.setDevice(a); cutorch.streamWaitFor(b, {streams...}).
  • cutorch.streamBarrier({streams...}): an N-to-N-way barrier between all the streams; all streams will wait for the completion of all other streams on the current device only. More efficient than creating the same N-to-N-way dependency via streamWaitFor.
  • cutorch.streamBarrierMultiDevice({[device]={streamsToWaitOn...}...}): As with streamBarrier but allows barriers between streams on arbitrary devices. Creates a cross-device N-to-N-way barrier between all (device, stream) values listed.
  • cutorch.streamSynchronize(stream): equivalent to cudaStreamSynchronize(stream) for the current device. Blocks the CPU until stream completes its queued kernels/events.
Common Examples

Transfering a FloatTensor src to the GPU:

dest = src:cuda() -- dest is on the current GPU

Allocating a tensor on a given GPU: Allocate src on GPU 3

cutorch.setDevice(3)
src = torch.CudaTensor(100)

Copying a CUDA tensor from one GPU to another: Given a tensor called src on GPU 1, if you want to create it's clone on GPU 2, then:

cutorch.setDevice(2)
local dest = src:clone()

OR

local dest
cutorch.withDevice(2, function() dest = src:clone() end)

cutorch's People

Contributors

adamlerer avatar ajtulloch avatar akfidjeland avatar andresy avatar bpiwowar avatar clementfarabet avatar colesbury avatar dominikgrewe avatar fmassa avatar hycis avatar ioannisantonoglou avatar jonathantompson avatar koraykv avatar leonbottou avatar nicholas-leonard avatar santazhang avatar soumith avatar szagoruyko avatar zakattacktwitter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.