{"action":"created","comment":{"url":"https://api.github.com/repos/torch/cutorch/pulls/comments/29057450","id":29057450,"diff_hunk":"@@ -184,9 +184,31 @@ void waitMultiDeviceEvents(lua_State *L, THCState *state, int arg,\n   lua_pop(L, 1);\n }\n \n+static int optSetDevice(lua_State *L, int param_idx, const char *func_name)\n+{\n+  THCState *state = cutorch_getstate(L);\n+  int oldDevice;\n+  THCudaCheck(cudaGetDevice(&oldDevice));\n+  int device = luaL_optint(L, param_idx, -10);\n+  if (device != -10) {\n+    THCState_setDevice(state, device - 1); /* zero indexed */\n+  } else if (THCState_getDeviceMode(state) != THCStateDeviceModeManual) {\n+      THError(\"%s: Current deviceID=0 and no device provided. Provide the deviceID as argument %d\",\n+          func_name, param_idx);\n+  }\n+  return oldDevice;\n+}\n+\n+/*\n+   Usage:\n+   cutorch.synchronize([device])\n+   Synchronizes the current or specified device with the host.\n+*/\n static int cutorch_synchronize(lua_State *L)\n {\n+  int oldDevice = optSetDevice(L, 1, \"synchronize\");\n   THCudaCheck(cudaDeviceSynchronize());\n+  THCState_setDevice(cutorch_getstate(L), oldDevice);","path":"init.c","position":28,"original_position":28,"commit_id":"26a1f0cb3460a22d29c36bc362b4e4a863749e05","original_commit_id":"fe6c28b47ee8dfa47484a04591f480e41d9d2816","user":{"login":"dominikgrewe","id":1619348,"avatar_url":"https://avatars.githubusercontent.com/u/1619348?v=3","gravatar_id":"","url":"https://api.github.com/users/dominikgrewe","html_url":"https://github.com/dominikgrewe","followers_url":"https://api.github.com/users/dominikgrewe/followers","following_url":"https://api.github.com/users/dominikgrewe/following{/other_user}","gists_url":"https://api.github.com/users/dominikgrewe/gists{/gist_id}","starred_url":"https://api.github.com/users/dominikgrewe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dominikgrewe/subscriptions","organizations_url":"https://api.github.com/users/dominikgrewe/orgs","repos_url":"https://api.github.com/users/dominikgrewe/repos","events_url":"https://api.github.com/users/dominikgrewe/events{/privacy}","received_events_url":"https://api.github.com/users/dominikgrewe/received_events","type":"User","site_admin":false},"body":"Actually, ignore that :) I thought we were in Lua, where doing this would be easy, but in C it would be a bit of a pain...","created_at":"2015-04-24T15:19:36Z","updated_at":"2015-04-24T15:19:36Z","html_url":"https://github.com/torch/cutorch/pull/136#discussion_r29057450","pull_request_url":"https://api.github.com/repos/torch/cutorch/pulls/136","_links":{"self":{"href":"https://api.github.com/repos/torch/cutorch/pulls/comments/29057450"},"html":{"href":"https://github.com/torch/cutorch/pull/136#discussion_r29057450"},"pull_request":{"href":"https://api.github.com/repos/torch/cutorch/pulls/136"}}},"pull_request":{"url":"https://api.github.com/repos/torch/cutorch/pulls/136","id":33594316,"html_url":"https://github.com/torch/cutorch/pull/136","diff_url":"https://github.com/torch/cutorch/pull/136.diff","patch_url":"https://github.com/torch/cutorch/pull/136.patch","issue_url":"https://api.github.com/repos/torch/cutorch/issues/136","number":136,"state":"open","locked":false,"title":"Auto device mode, plus allocation helper functions.","user":{"login":"adamlerer","id":5702157,"avatar_url":"https://avatars.githubusercontent.com/u/5702157?v=3","gravatar_id":"","url":"https://api.github.com/users/adamlerer","html_url":"https://github.com/adamlerer","followers_url":"https://api.github.com/users/adamlerer/followers","following_url":"https://api.github.com/users/adamlerer/following{/other_user}","gists_url":"https://api.github.com/users/adamlerer/gists{/gist_id}","starred_url":"https://api.github.com/users/adamlerer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamlerer/subscriptions","organizations_url":"https://api.github.com/users/adamlerer/orgs","repos_url":"https://api.github.com/users/adamlerer/repos","events_url":"https://api.github.com/users/adamlerer/events{/privacy}","received_events_url":"https://api.github.com/users/adamlerer/received_events","type":"User","site_admin":false},"body":"This diff introduces an alternative way of writing multi-GPU cutorch\r\ncode. In this mode, the location of each tensor is specified, and the\r\nappropriate GPU for each kernel is determined automatically based on the\r\nlocation of its argument tensors. It's backwards-compatible and interoperable\r\nwith the old-style multi-GPU API.\r\n\r\n-------------------------------------------------------\r\n\r\nCalling cutorch.setDevice(0) causes cutorch to automatically use the appropriate device for a given computation. Non-empty tensors cannot be allocated in this mode, unless one of the following two convenience methods is used:\r\n\r\n    torch.CudaDeviceOn(device, ...)\r\n    torch.FloatTensor(...)):cuda(device)\r\n\r\nExample:\r\n\r\n    > cutorch.setDevice(0)  -- 'auto' or 'no device'\r\n    > a = CudaTensorOn(1, 10)  -- size 10, on device 1\r\n    > b = CudaTensorOn(2, 10)  -- size 10, on device 2\r\n    > c = CudaTensor() -- size 0, unallocated\r\n    > c:add(a, 1)   -- c is now size 1, on device 1\r\n    > d = a + b\r\n    Error: tensors are on different devices\r\n\r\nThe old behavior w.r.t empty tensors is preserved - they are unallocated, and are assigned a device only when resized to non-zero size. However, you can now assign an empty tensor to a device, using either t:setDevice(device) or one of the convenience methods above, and in device=0 mode it will retain its assigned device upon resize.\r\n\r\n@dominikgrewe can you take a look? @soumith suggested I get your feedback. Thanks!\r\n","created_at":"2015-04-18T01:42:24Z","updated_at":"2015-04-24T15:19:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8a7531affdbc6e8ab84b7dbf02f07f12642ebd4a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/torch/cutorch/pulls/136/commits","review_comments_url":"https://api.github.com/repos/torch/cutorch/pulls/136/comments","review_comment_url":"https://api.github.com/repos/torch/cutorch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/torch/cutorch/issues/136/comments","statuses_url":"https://api.github.com/repos/torch/cutorch/statuses/26a1f0cb3460a22d29c36bc362b4e4a863749e05","head":{"label":"adamlerer:auto_device","ref":"auto_device","sha":"26a1f0cb3460a22d29c36bc362b4e4a863749e05","user":{"login":"adamlerer","id":5702157,"avatar_url":"https://avatars.githubusercontent.com/u/5702157?v=3","gravatar_id":"","url":"https://api.github.com/users/adamlerer","html_url":"https://github.com/adamlerer","followers_url":"https://api.github.com/users/adamlerer/followers","following_url":"https://api.github.com/users/adamlerer/following{/other_user}","gists_url":"https://api.github.com/users/adamlerer/gists{/gist_id}","starred_url":"https://api.github.com/users/adamlerer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamlerer/subscriptions","organizations_url":"https://api.github.com/users/adamlerer/orgs","repos_url":"https://api.github.com/users/adamlerer/repos","events_url":"https://api.github.com/users/adamlerer/events{/privacy}","received_events_url":"https://api.github.com/users/adamlerer/received_events","type":"User","site_admin":false},"repo":{"id":34131371,"name":"cutorch","full_name":"adamlerer/cutorch","owner":{"login":"adamlerer","id":5702157,"avatar_url":"https://avatars.githubusercontent.com/u/5702157?v=3","gravatar_id":"","url":"https://api.github.com/users/adamlerer","html_url":"https://github.com/adamlerer","followers_url":"https://api.github.com/users/adamlerer/followers","following_url":"https://api.github.com/users/adamlerer/following{/other_user}","gists_url":"https://api.github.com/users/adamlerer/gists{/gist_id}","starred_url":"https://api.github.com/users/adamlerer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamlerer/subscriptions","organizations_url":"https://api.github.com/users/adamlerer/orgs","repos_url":"https://api.github.com/users/adamlerer/repos","events_url":"https://api.github.com/users/adamlerer/events{/privacy}","received_events_url":"https://api.github.com/users/adamlerer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/adamlerer/cutorch","description":"A CUDA backend for Torch7","fork":true,"url":"https://api.github.com/repos/adamlerer/cutorch","forks_url":"https://api.github.com/repos/adamlerer/cutorch/forks","keys_url":"https://api.github.com/repos/adamlerer/cutorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/adamlerer/cutorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/adamlerer/cutorch/teams","hooks_url":"https://api.github.com/repos/adamlerer/cutorch/hooks","issue_events_url":"https://api.github.com/repos/adamlerer/cutorch/issues/events{/number}","events_url":"https://api.github.com/repos/adamlerer/cutorch/events","assignees_url":"https://api.github.com/repos/adamlerer/cutorch/assignees{/user}","branches_url":"https://api.github.com/repos/adamlerer/cutorch/branches{/branch}","tags_url":"https://api.github.com/repos/adamlerer/cutorch/tags","blobs_url":"https://api.github.com/repos/adamlerer/cutorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/adamlerer/cutorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/adamlerer/cutorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/adamlerer/cutorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/adamlerer/cutorch/statuses/{sha}","languages_url":"https://api.github.com/repos/adamlerer/cutorch/languages","stargazers_url":"https://api.github.com/repos/adamlerer/cutorch/stargazers","contributors_url":"https://api.github.com/repos/adamlerer/cutorch/contributors","subscribers_url":"https://api.github.com/repos/adamlerer/cutorch/subscribers","subscription_url":"https://api.github.com/repos/adamlerer/cutorch/subscription","commits_url":"https://api.github.com/repos/adamlerer/cutorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/adamlerer/cutorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/adamlerer/cutorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/adamlerer/cutorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/adamlerer/cutorch/contents/{+path}","compare_url":"https://api.github.com/repos/adamlerer/cutorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/adamlerer/cutorch/merges","archive_url":"https://api.github.com/repos/adamlerer/cutorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/adamlerer/cutorch/downloads","issues_url":"https://api.github.com/repos/adamlerer/cutorch/issues{/number}","pulls_url":"https://api.github.com/repos/adamlerer/cutorch/pulls{/number}","milestones_url":"https://api.github.com/repos/adamlerer/cutorch/milestones{/number}","notifications_url":"https://api.github.com/repos/adamlerer/cutorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/adamlerer/cutorch/labels{/name}","releases_url":"https://api.github.com/repos/adamlerer/cutorch/releases{/id}","created_at":"2015-04-17T17:50:21Z","updated_at":"2015-04-17T17:50:22Z","pushed_at":"2015-04-23T18:32:58Z","git_url":"git://github.com/adamlerer/cutorch.git","ssh_url":"git@github.com:adamlerer/cutorch.git","clone_url":"https://github.com/adamlerer/cutorch.git","svn_url":"https://github.com/adamlerer/cutorch","homepage":"","size":658,"stargazers_count":0,"watchers_count":0,"language":"Cuda","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"torch:master","ref":"master","sha":"0903d5763025d163c575b11d5a51ce3d760c3ad1","user":{"login":"torch","id":4978924,"avatar_url":"https://avatars.githubusercontent.com/u/4978924?v=3","gravatar_id":"","url":"https://api.github.com/users/torch","html_url":"https://github.com/torch","followers_url":"https://api.github.com/users/torch/followers","following_url":"https://api.github.com/users/torch/following{/other_user}","gists_url":"https://api.github.com/users/torch/gists{/gist_id}","starred_url":"https://api.github.com/users/torch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torch/subscriptions","organizations_url":"https://api.github.com/users/torch/orgs","repos_url":"https://api.github.com/users/torch/repos","events_url":"https://api.github.com/users/torch/events{/privacy}","received_events_url":"https://api.github.com/users/torch/received_events","type":"Organization","site_admin":false},"repo":{"id":13677478,"name":"cutorch","full_name":"torch/cutorch","owner":{"login":"torch","id":4978924,"avatar_url":"https://avatars.githubusercontent.com/u/4978924?v=3","gravatar_id":"","url":"https://api.github.com/users/torch","html_url":"https://github.com/torch","followers_url":"https://api.github.com/users/torch/followers","following_url":"https://api.github.com/users/torch/following{/other_user}","gists_url":"https://api.github.com/users/torch/gists{/gist_id}","starred_url":"https://api.github.com/users/torch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torch/subscriptions","organizations_url":"https://api.github.com/users/torch/orgs","repos_url":"https://api.github.com/users/torch/repos","events_url":"https://api.github.com/users/torch/events{/privacy}","received_events_url":"https://api.github.com/users/torch/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/torch/cutorch","description":"A CUDA backend for Torch7","fork":false,"url":"https://api.github.com/repos/torch/cutorch","forks_url":"https://api.github.com/repos/torch/cutorch/forks","keys_url":"https://api.github.com/repos/torch/cutorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/torch/cutorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/torch/cutorch/teams","hooks_url":"https://api.github.com/repos/torch/cutorch/hooks","issue_events_url":"https://api.github.com/repos/torch/cutorch/issues/events{/number}","events_url":"https://api.github.com/repos/torch/cutorch/events","assignees_url":"https://api.github.com/repos/torch/cutorch/assignees{/user}","branches_url":"https://api.github.com/repos/torch/cutorch/branches{/branch}","tags_url":"https://api.github.com/repos/torch/cutorch/tags","blobs_url":"https://api.github.com/repos/torch/cutorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/torch/cutorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/torch/cutorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/torch/cutorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/torch/cutorch/statuses/{sha}","languages_url":"https://api.github.com/repos/torch/cutorch/languages","stargazers_url":"https://api.github.com/repos/torch/cutorch/stargazers","contributors_url":"https://api.github.com/repos/torch/cutorch/contributors","subscribers_url":"https://api.github.com/repos/torch/cutorch/subscribers","subscription_url":"https://api.github.com/repos/torch/cutorch/subscription","commits_url":"https://api.github.com/repos/torch/cutorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/torch/cutorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/torch/cutorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/torch/cutorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/torch/cutorch/contents/{+path}","compare_url":"https://api.github.com/repos/torch/cutorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/torch/cutorch/merges","archive_url":"https://api.github.com/repos/torch/cutorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/torch/cutorch/downloads","issues_url":"https://api.github.com/repos/torch/cutorch/issues{/number}","pulls_url":"https://api.github.com/repos/torch/cutorch/pulls{/number}","milestones_url":"https://api.github.com/repos/torch/cutorch/milestones{/number}","notifications_url":"https://api.github.com/repos/torch/cutorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/torch/cutorch/labels{/name}","releases_url":"https://api.github.com/repos/torch/cutorch/releases{/id}","created_at":"2013-10-18T12:29:23Z","updated_at":"2015-04-21T22:18:43Z","pushed_at":"2015-04-23T18:32:59Z","git_url":"git://github.com/torch/cutorch.git","ssh_url":"git@github.com:torch/cutorch.git","clone_url":"https://github.com/torch/cutorch.git","svn_url":"https://github.com/torch/cutorch","homepage":"","size":2214,"stargazers_count":34,"watchers_count":34,"language":"Cuda","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":8,"forks":34,"open_issues":8,"watchers":34,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/torch/cutorch/pulls/136"},"html":{"href":"https://github.com/torch/cutorch/pull/136"},"issue":{"href":"https://api.github.com/repos/torch/cutorch/issues/136"},"comments":{"href":"https://api.github.com/repos/torch/cutorch/issues/136/comments"},"review_comments":{"href":"https://api.github.com/repos/torch/cutorch/pulls/136/comments"},"review_comment":{"href":"https://api.github.com/repos/torch/cutorch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/torch/cutorch/pulls/136/commits"},"statuses":{"href":"https://api.github.com/repos/torch/cutorch/statuses/26a1f0cb3460a22d29c36bc362b4e4a863749e05"}}}}