{"action":"created","issue":{"url":"https://api.github.com/repos/Idein/qmkl/issues/45","repository_url":"https://api.github.com/repos/Idein/qmkl","labels_url":"https://api.github.com/repos/Idein/qmkl/issues/45/labels{/name}","comments_url":"https://api.github.com/repos/Idein/qmkl/issues/45/comments","events_url":"https://api.github.com/repos/Idein/qmkl/issues/45/events","html_url":"https://github.com/Idein/qmkl/pull/45","id":293402730,"number":45,"title":"Use VCSM for memory allocation","user":{"login":"Terminus-IMRC","id":4298265,"avatar_url":"https://avatars2.githubusercontent.com/u/4298265?v=4","gravatar_id":"","url":"https://api.github.com/users/Terminus-IMRC","html_url":"https://github.com/Terminus-IMRC","followers_url":"https://api.github.com/users/Terminus-IMRC/followers","following_url":"https://api.github.com/users/Terminus-IMRC/following{/other_user}","gists_url":"https://api.github.com/users/Terminus-IMRC/gists{/gist_id}","starred_url":"https://api.github.com/users/Terminus-IMRC/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Terminus-IMRC/subscriptions","organizations_url":"https://api.github.com/users/Terminus-IMRC/orgs","repos_url":"https://api.github.com/users/Terminus-IMRC/repos","events_url":"https://api.github.com/users/Terminus-IMRC/events{/privacy}","received_events_url":"https://api.github.com/users/Terminus-IMRC/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-01T04:48:01Z","updated_at":"2018-02-02T06:27:08Z","closed_at":null,"author_association":"MEMBER","pull_request":{"url":"https://api.github.com/repos/Idein/qmkl/pulls/45","html_url":"https://github.com/Idein/qmkl/pull/45","diff_url":"https://github.com/Idein/qmkl/pull/45.diff","patch_url":"https://github.com/Idein/qmkl/pull/45.patch"},"body":"With this PR, VCSM (VideoCore Shared Memory) service is used for memory allocation. VCSM is a service provided by the official Raspberry Pi firmware and kernel.\r\n\r\nBefore using vcsm, shared memory was cached only on QPU, and accessing it from CPU was so slow. With vcsm, shared memory is now cached on both CPU and QPU, and accessing from CPU gets much faster:\r\n\r\n```\r\n$ test/vcsm\r\nRead test. Using size=33.5544[MB]. Measuring 4 times each.\r\ncached     #0: 0.053792 [s], 6.23781e+08 [B/s]\r\nnon-cached #0: 1.11723 [s], 3.00336e+07 [B/s]\r\ncached     #1: 0.039932 [s], 8.40289e+08 [B/s]\r\nnon-cached #1: 1.10374 [s], 3.04008e+07 [B/s]\r\ncached     #2: 0.0399859 [s], 8.39157e+08 [B/s]\r\nnon-cached #2: 1.10367 [s], 3.04026e+07 [B/s]\r\ncached     #3: 0.0399311 [s], 8.40309e+08 [B/s]\r\nnon-cached #3: 1.10367 [s], 3.04025e+07 [B/s]\r\n\r\nWrite test. Using size=33.5544[MB]. Measuring 4 times each.\r\ncached     #0: 0.041698 [s], 8.04702e+08 [B/s]\r\nnon-cached #0: 0.0555301 [s], 6.04257e+08 [B/s]\r\ncached     #1: 0.0291049 [s], 1.15288e+09 [B/s]\r\nnon-cached #1: 0.045815 [s], 7.3239e+08 [B/s]\r\ncached     #2: 0.0290599 [s], 1.15466e+09 [B/s]\r\nnon-cached #2: 0.0460608 [s], 7.28481e+08 [B/s]\r\ncached     #3: 0.0291231 [s], 1.15216e+09 [B/s]\r\nnon-cached #3: 0.0460551 [s], 7.28572e+08 [B/s]\r\n```\r\n\r\n## Note 1\r\n\r\nvcsm operations are done through `/dev/vcsm` device and mailbox operations are done through `/dev/vcio`, and both devices are owned by `root.video` and the permission is `rw-rw----`. So QMKL users now need to be  ***either*** of these:\r\n\r\n* Be `root` user.\r\n* Be in `video` group.\r\n\r\n## Note 2\r\n\r\nNow memory allocated with `mkl_malloc()` is cache-enabled on both CPU and QPU. To keep coherency of such memory, users now need to clear/invalidate cache explicitly with `qmkl_cache_op_*()` functions. You don't need to do this if you ***only*** use QMKL's BLAS functions, because they do that by themselves. Or you can use `mkl_malloc_cache(size, align, 0)` to allocate cache-disabled memory.\r\n\r\n## Note 3\r\n\r\nBecause the cache operation is done through `ioctl()` call and takes some time, there is a little overhead on the BLAS functions. For example:\r\n\r\nBefore using vcsm:\r\n```\r\n$ sudo test/sgemm\r\n...\r\nGPU: 0.025985 [s], 8.27365e+09 [flop/s]\r\n...\r\n```\r\n\r\nAfter using VCSM:\r\n```\r\n$ test/sgemm\r\n...\r\nGPU: 0.026907 [s], 7.99015e+09 [flop/s]\r\n...\r\n```\r\n\r\nIf you rarely access the memory from CPU, I recommend you to allocate cache-disabled memory by using `mkl_malloc_cache(size, align, 0)` call.\r\n\r\n## Note 4\r\n\r\nYou cannot use this vcsm feature before https://github.com/raspberrypi/firmware/commit/58395c1ea3f9ae129885aa7ff7d8b92c7be267db commit just 10 hours ago. So please run `rpi-update` before testing this PR."},"comment":{"url":"https://api.github.com/repos/Idein/qmkl/issues/comments/362498476","html_url":"https://github.com/Idein/qmkl/pull/45#issuecomment-362498476","issue_url":"https://api.github.com/repos/Idein/qmkl/issues/45","id":362498476,"user":{"login":"notogawa","id":372530,"avatar_url":"https://avatars1.githubusercontent.com/u/372530?v=4","gravatar_id":"","url":"https://api.github.com/users/notogawa","html_url":"https://github.com/notogawa","followers_url":"https://api.github.com/users/notogawa/followers","following_url":"https://api.github.com/users/notogawa/following{/other_user}","gists_url":"https://api.github.com/users/notogawa/gists{/gist_id}","starred_url":"https://api.github.com/users/notogawa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/notogawa/subscriptions","organizations_url":"https://api.github.com/users/notogawa/orgs","repos_url":"https://api.github.com/users/notogawa/repos","events_url":"https://api.github.com/users/notogawa/events{/privacy}","received_events_url":"https://api.github.com/users/notogawa/received_events","type":"User","site_admin":false},"created_at":"2018-02-02T06:27:08Z","updated_at":"2018-02-02T06:27:08Z","author_association":"CONTRIBUTOR","body":"LGTM, nice work."}}
{"actor":{"display_login":"notogawa"}}