<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 許友綸 on Medium]]></title>
        <description><![CDATA[Stories by 許友綸 on Medium]]></description>
        <link>https://medium.com/@tim37021?source=rss-90e1d5dd5600------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YKPDPywzMFQk0jrU.jpg</url>
            <title>Stories by 許友綸 on Medium</title>
            <link>https://medium.com/@tim37021?source=rss-90e1d5dd5600------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:31:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tim37021/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building FFMpeg with NDK r21e]]></title>
            <link>https://tim37021.medium.com/building-ffmpeg-with-ndk-r21e-f7cd172bdfa0?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7cd172bdfa0</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[ffmpeg]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Sat, 12 Jun 2021 14:59:22 GMT</pubDate>
            <atom:updated>2021-06-12T15:00:40.535Z</atom:updated>
            <content:encoded><![CDATA[<p>There’s a lot of ffmpeg build script out there you can find on the Internet. But I can find none of them using r21e with FFMpeg 4.3. So, let’s build it.</p><p>It seems r21e has different C compiler path. clang is separated from gcc toolchains.</p><pre>CC=$CLANG_TOOLCHAIN/bin/aarch64-linux-android30-clang<br>CXX=$CLANG_TOOLCHAIN/bin/aarch64-linux-android30-clang++</pre><p>The configure is not that hard, although the build process takes really long. Since we are using libav only, I disable all standalone program like ffmpeg, ffplay.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5244d6242b4f615385df6d033ad1bdbe/href">https://medium.com/media/5244d6242b4f615385df6d033ad1bdbe/href</a></iframe><p>Those lovely libxxxx.a file will reside in ./android/arm64.</p><p>If you are using Qt Creator. API Level Should changed to at least 28. Contact me if you need pre-compiled binary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7cd172bdfa0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Kotlin in Your Qt Project]]></title>
            <link>https://tim37021.medium.com/using-kotlin-in-your-qt-project-299398c5b617?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/299398c5b617</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[qt]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Thu, 04 Mar 2021 15:21:11 GMT</pubDate>
            <atom:updated>2021-03-04T15:25:03.079Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MGd6bpJkbWmWFf7U.png" /></figure><p>The story starts here: kotlin has been out for a while. But Qt’s kotlin support is zero. It will scare away many beginners</p><h4>Add Kotlin Support in Existing Qt Project</h4><p>There’s no template project for Kotlin development in Qt. For those who are not familiar with Gradle or Qt for Android. Here I provide the instructions to create Kotlin+Qt android project with minimum modification to original template.</p><ul><li>Step 1 — Create Template</li></ul><p>Navigate to Projects/Build/Build Steps. Click the “Create Templates” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vtc1OQm3vbTSWO8_w7qU_g.png" /></figure><p>Now you should have a new folder, <strong>android</strong>, in your project.</p><ul><li>Step 2 — Modify build.gradle</li></ul><ol><li>In the buildscript block add kotlin version def.</li></ol><pre>buildscript {<br>+   ext.kotlin_version = &quot;1.3.72&quot;</pre><p>2. Specify kotlin classpath inside dependencies block</p><pre>dependencies {<br>        classpath &#39;com.android.tools.build:gradle:3.6.0&#39;<br>+       classpath &quot;org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version&quot;<br>    }</pre><p>3. Add kotlin plugin, you can put it together with other apply plugin statement</p><pre>apply plugin: &#39;kotlin-android&#39;</pre><p>4. Add kotlin sourceSet. Notice, you may or may not want to mix Java with Kotlin. It’s up to your decision. Here I separate Kotlin and Java source code.</p><pre>sourceSets {<br>    main {<br>        manifest.srcFile &#39;AndroidManifest.xml&#39;<br>        java.srcDirs = [qt5AndroidDir + &#39;/src&#39;, &#39;src/java&#39;, &#39;java&#39;]<br>        aidl.srcDirs = [qt5AndroidDir + &#39;/src&#39;, &#39;src&#39;, &#39;aidl&#39;]<br>+       kotlin.srcDirs = [&#39;src/kotlin&#39;, &#39;kotlin&#39;]<br>        res.srcDirs = [qt5AndroidDir + &#39;/res&#39;, &#39;res&#39;]<br>        resources.srcDirs = [&#39;resources&#39;]<br>        renderscript.srcDirs = [&#39;src&#39;]<br>        assets.srcDirs = [&#39;assets&#39;]<br>        jniLibs.srcDirs = [&#39;libs&#39;]<br>   }<br>}</pre><p>5. Add kotlin implementation in the very last line.</p><pre>dependencies {<br>+   implementation &quot;org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version&quot;<br>    implementation &#39;com.github.felHR85:UsbSerial:6.1.0&#39;<br>}</pre><h4>Interop with Kotlin</h4><p>It’s not too hard to add Kotlin in your Qt project. How about interacting with your Kotlin class? Actually it is same as interacting with Java, since Kotlin is also compiled to JVM bytecode.</p><p>With above gradle script, if you have</p><pre>projectRoot/android/src/kotlin/com/company/package/MyClass.kt</pre><p>Then the classPath of MyClass is</p><pre>com/company/package/MyClass</pre><p>You can interact with MyClass with JNI or Qt’s JNI wrapper called QAndroidJniObject</p><h4>Use Static Method</h4><p>Kotlin has companion object concept. It’s similar to java’s static method but there’re <a href="https://stackoverflow.com/questions/48780003/why-and-when-to-use-jvmstatic-with-companion-objects">something different</a>. Use <em>javap </em>to view MyClass.class you will see the different. Luckily we can mark <em>@JvmStatic, </em>so that it can really mimic java static method and still make your kotlin clean.</p><pre>class MyClass {<br>    companion object {<br>        <a href="http://twitter.com/JvmStatic">@JvmStatic</a> fun myStaticFunction() {}<br>    }<br>}</pre><h4>View Function Signature without Javap</h4><p>If you have experience with NDK, then frequently checking method signature with <em>javap </em>is really aweful. Especially output path of compiled kotlin and java are different. I find that <em>Show Kotlin Bytecode </em>in Android Studio is quite useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aIVSvixiFnbRp-4aNo6SFg.png" /></figure><p>You can quickly find the signature in the compiled bytecode. Here the signature of onCreate is (Landroid/os/Bundle;)V</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*ZeieBg06H_jOx7gioNrLVQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=299398c5b617" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[String Searching in Linear Complexity— Knuth–Morris–Pratt Algorithm]]></title>
            <link>https://tim37021.medium.com/string-searching-in-linear-complexity-knuth-morris-pratt-algorithm-9fc4750035b3?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fc4750035b3</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Wed, 20 Jan 2021 09:22:13 GMT</pubDate>
            <atom:updated>2021-01-20T09:53:16.866Z</atom:updated>
            <content:encoded><![CDATA[<h3>String Searching in O(N)</h3><p>For string searching, it is easy to come up with a naive solution for string searching</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0adf1572a514f0554fa76e663a13a3f/href">https://medium.com/media/f0adf1572a514f0554fa76e663a13a3f/href</a></iframe><p>However the naive solution takes O(MN), where N and M denote the length of str and pattern respectively. And often, we meet the worst case.</p><pre>str=aaaaaaaaaaaaaaaa; pattern=aaaaaaaab</pre><p>for each position i in str, we need to compare the pattern ‘til the end.</p><p>The intuition is that we have known that the prefix a* is already compared and matched. We only need to make sure if the next one is b. Take a look at more example</p><pre>str=abcd[e]; pattern=abcd[f]<br>// strstr(str, pattern) == [];</pre><p>after comparing e and f, is it really necessary to restart the comparison at position 1? NO, we can forward the starting position to the position of e. In the sense that we need to take care of the repeated structure of the pattern string.</p><h3>Knuth–Morris–Pratt Algorithm</h3><ol><li>Preprocess the pattern, calculate lps array</li></ol><pre>lps[i] := the longest length of the proper prefix of str[0…i] that matches the postfix of str[0…i].<br>“proper prefix” means all prefixes excluding the whole string.</pre><p>For example lps(“AABAAB”) == [0, 1, 0, 1, 2, 3], lps(“AAA”) = [0, 1, 2]</p><p>Calculate lsp is trivial. We can use dynamic programming here. I’ve seen so many O(M²) code.</p><p>2. KMP match</p><pre>initialize i=0 and j=0, calculate lps(pattern)</pre><pre>while i&lt;len(str)<br>    if str[i] == pattern[j]  // MATCH<br>        j++<br>    else if j &gt; 0            // MISMATCH and j&gt;0<br>        j = lps[j-1]<br>        continue</pre><pre>    if j &gt;= len(pattern)     // A COMPLETE MATCH<br>        yield i-j+1<br>    i++</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66931239201da68e9b0ef4975973e0a7/href">https://medium.com/media/66931239201da68e9b0ef4975973e0a7/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fc4750035b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CMake Server]]></title>
            <link>https://tim37021.medium.com/cmake-server-c468e55437f4?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/c468e55437f4</guid>
            <category><![CDATA[computer-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cmake]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Sun, 02 Feb 2020 12:50:13 GMT</pubDate>
            <atom:updated>2024-07-31T17:28:07.604Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been using cmake+vscode workflow for years. Both cpptool and cmake-tool work well and they interlop perfectly. Recently, I started heavy development of embedded device. It seems cmake-tool is kind of buggy, but I don’t want to abandon cmake workflow. So I started reading the document of cmake server to extract the configuration of the project especially includePath.</p><p>You can start server by following command</p><pre>cmake -E server --debug --experimental</pre><p>When you start the server, it will print its protocol version to standard output. notice an empty line before the response text.</p><pre><br>[== &quot;CMake Server&quot; ==[<br>{&quot;supportedProtocolVersions&quot;:[{&quot;isExperimental&quot;:true,&quot;major&quot;:1,&quot;minor&quot;:2}],&quot;type&quot;:&quot;hello&quot;}<br>]== &quot;CMake Server&quot; ==]</pre><p>Now do a handshake to continue, send the following request to the server via stdin. Note that the protocolVersion should match the previous response.</p><pre>[== &quot;CMake Server&quot; ==[<br>{&quot;cookie&quot;:&quot;zimtstern&quot;,&quot;type&quot;:&quot;handshake&quot;,&quot;protocolVersion&quot;:{&quot;major&quot;:1, &quot;minor&quot;: 2},<br> &quot;sourceDirectory&quot;:&quot;C:/Users/xxxx/Desktop/Test&quot;, &quot;buildDirectory&quot;:&quot;C:/Users/xxxx/Desktop/Test/build&quot;,<br> &quot;generator&quot;:&quot;Ninja&quot;}<br>]== &quot;CMake Server&quot; ==]<br></pre><p>We can fetch cmake cache with the following command “cache”. This is the general form of a command with no argument.</p><pre>[== &quot;CMake Server&quot; ==[<br>{&quot;type&quot;:&quot;cache&quot;}<br>]== &quot;CMake Server&quot; ==]<br></pre><p>for extracting predefined macro and include path. Sequentially send configure, compute, codemodel. You can find macro and include path in the response of codemodel. Note that cmake server will send progress asynchronously during configure and compute stage.</p><p>Here I provide a python script for dumping codemodel. Don’t blame me for the code structure, I wrote this in 10 minutes only for retrieving data…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f7cea38a53a3f924d314ae13632fed/href">https://medium.com/media/64f7cea38a53a3f924d314ae13632fed/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c468e55437f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PyCUDA/CuPy PyTorch Interlop]]></title>
            <link>https://tim37021.medium.com/pycuda-cupy-pytorch-interlop-56a9875e2aa7?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/56a9875e2aa7</guid>
            <category><![CDATA[cuda]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Fri, 22 Nov 2019 09:41:18 GMT</pubDate>
            <atom:updated>2019-11-22T09:45:24.265Z</atom:updated>
            <content:encoded><![CDATA[<p>Since I need fast autograd for my research, and PyTorch naturally becomes my best choice. Calculating stuff in batch make my code more elegant. Some are harder to achieve, especially when the if-else clause involved in.</p><p>PyCUDA is one of CUDA binding for python. Since it creates its own CUDA context, we should do some dirty trick to share context with pytorch. I found this solution somewhere(stackoverflow?)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49d5b422fd9134eb692d8f61db6b006e/href">https://medium.com/media/49d5b422fd9134eb692d8f61db6b006e/href</a></iframe><p>There is another choice to execute CUDA kernel in Python: CuPy. It aims to provide Numpy like array and serves as a back-end of neural network API, <a href="https://chainer.org/">Chainer</a>, which is the first framework to introduce dynamic graph creation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfce1160088ec38405c698f4cd8ecd90/href">https://medium.com/media/bfce1160088ec38405c698f4cd8ecd90/href</a></iframe><p>Here I list a minimal example to show how we can run kernel with tensors created by PyTorch. The key is to provide CUDA stream to the CuPy wrapped function. Note that this time we execute pre-compiled kernel.</p><p>By the way, remember to add extern “C” to the functions you exports to Python. Otherwise, C++ name mangling…..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56a9875e2aa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[《薄伽梵歌》讀後心得]]></title>
            <link>https://tim37021.medium.com/%E8%96%84%E4%BC%BD%E6%A2%B5%E6%AD%8C-%E8%AE%80%E5%BE%8C%E5%BF%83%E5%BE%97-9aabef034669?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/9aabef034669</guid>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[bhagavad-gita]]></category>
            <category><![CDATA[mindfulness]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Sat, 29 Jun 2019 10:06:24 GMT</pubDate>
            <atom:updated>2019-06-29T10:06:24.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/348/0*RgNn-t0kEUZQpsbM" /></figure><p>有發現自己比較適合閱讀字少少的哲學書，可以讓我在腦中細細品味。《薄伽梵歌》只有七百行就特別適合我！不過當初會拿來閱讀只是聽說愛因斯坦明明是無神論者，他卻也讀過這本書，而這本書是宗教的經典。同為無神論者的我也很想要一探究竟！</p><p>本書的故事背景是阿朱那面對持國諸子而怯戰，在一旁的克里希那給予一個靈性的教誨。學習如何不執著，無私的付出，享受行動當下即刻的果實。</p><p>小時候師長總是教導要不生氣，這壓抑了我們自然的情緒，華人總是重視表面上的和諧，但內心早已掀起巨大波瀾，等到衝突發生之時往往已經無可挽回。不生氣其實是不執著的一部分，但我們往往只理解表面上的意思，這容易落入”執著於不執著”的陷阱之中。</p><p>物質社會充斥的現代生活，精神生活的論述總是被非常表面的解讀成一種幹話。真理很有趣，它是一種覺知的知識，也可以說一種感覺或思考脈絡，他不能很容易的言傳。我還記得海濤法師前陣子爭議性的&quot;劈腿都是假的!&quot;，這大概是在說「空性」吧！其實這是一個看似消極的積極論述。劈腿或是無縫發生的時候，其實感情大概已經生變很久了，最後人往往只在意那個具體事件，留下滿滿的憎恨，長期下來那樣的憎恨繼續荼毒自己的身心。</p><blockquote>事件本身是中性的，沒有對錯好壞！</blockquote><p>你無法接受嗎？這樣想好了，事發當下也許是別人的錯，但在事發之後我們的情緒管控就是自己的問題了。人對事件的認知來自於個人的主觀，本書也有說我們必須超越成敗的看待事情(化解二元對立)</p><blockquote>得就是失，失就是得</blockquote><p>道德觀不停的告訴我們什麼是對的什麼是好的什麼是成功的，其實這反而會加深我們對事件的執著。所有的事情都具有正反兩面，而我們往往只看到事情的其中一面。可能我們在創業成功之時會得大頭症，忘了自己失去陪伴家人的時間，在失敗的時候會一蹶不振，卻忘了自己獲得了個寶貴的經驗。很多時候我們只想要得，所以才會痛苦萬分。但是這本身是違反自然法則的</p><p>提倡不執著，也是提倡一種寧靜自在的生活觀。怎麼做呢？怎麼樣做事不綁手綁腳，而能做出全然自由的行動呢？其實這是之前的我一直想獲得的答案。這本書就說，把行動想成一種獻祭，所有行動都是奉獻給上帝，就能達成無私。其實就是Just Do It，會綁手綁腳是因為我們對一直成功有所執著，但是又有誰只會成功呢？「享受行動即刻的果實」也就是覺察與活在當下，而非期待未來的快樂</p><p>讀到這邊我恍然大悟，常常我們會看到有人拿貴重金子去拜拜，想說這樣就可以獲得更強的成效，我才發現，經書根本就不是這樣說，也許帶著執念去信宗教，那就會獲得滿滿的執念的宗教觀吧。原來上帝不是讓自己心想事成的上帝，造物者只創造萬物的和諧。人與上帝，如同i之於旋轉與傅立葉轉換，i是什麼？我們看不到，但它開啟了我們新的視野。</p><p>若以「心靈和諧，歲月靜好」為本，那還真的有一條路給你走。我還記得有一天，我的同學跑來跟我說：「欸，我剛讀六祖禪經，很多跟你說的很像欸！」其實進而確認之後，發現幾乎是等價的！</p><p>也許真理是超越宗教的吧！唯有還沒悟得的人，才會覺得世界上有很多宗教。從歷史下去看，宗教造成非常多的麻煩。但宗教仍然有其存在的必要，畢竟每個人生長環境不同，可以接受的悟道途徑也不同，也就是每個人需要有不同的導師。以一個例子來說，我們傾向於相信與自己有類似處境的人講出的話，小時候我被霸凌，媽媽就叫我檢討自己？當下只覺得我何錯之有?「你不是我，你不懂啦。」在了悟這些之後，我才發現原來我媽媽早就跟我說過了！現在甚至認為，相同處境的人有時候也不見得能給予適當的建議。書本章節的導讀也有說</p><blockquote>在一個還沒了悟的人面前，也許上帝站在他旁邊他也不會發現吧。</blockquote><p>其實再寫下去根本寫不完，《薄伽梵歌》某種程度上完全解除了我長期內心的衝突，向外求永遠求不得又陷自己於不利，那只能試著往內看，再從自我覺察了解整個宇宙。《翠玉錄》也有說：</p><blockquote>as the universe, so the soul.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9aabef034669" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PyCUDA 和 PyTorch交互]]></title>
            <link>https://tim37021.medium.com/pycuda-%E5%92%8C-pytorch%E4%BA%A4%E4%BA%92-29688ba26ea6?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/29688ba26ea6</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[cuda]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Sat, 18 May 2019 07:00:34 GMT</pubDate>
            <atom:updated>2019-05-18T07:00:34.830Z</atom:updated>
            <content:encoded><![CDATA[<p>最近這些日子需要用到自動微分選擇了pytorch，batch運算有時候很爽快，只不過依然有某些操作實在很難寫，尤其是batch運算裡面有if-else的。比如以下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/550241c82bec6e9721a99a35e199a6e1/href">https://medium.com/media/550241c82bec6e9721a99a35e199a6e1/href</a></iframe><p>這段是針對一個ray，如果是batch of rays實在很難改…</p><p>於是就想到用CUDA，不過缺點就是這時候gradient要自己手算了，之後我會再介紹怎麼樣寫autograd。</p><p>PyCUDA按字面意思就是python的cuda binding。只是他當初設計時並沒有考慮跟其他lib交互…所以他會自己創一個CUDA Context，我們得用髒髒的方式讓他跟Pytorch共用Context，就省下GPU-&gt;CPU-&gt;GPU的垃圾途徑。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49d5b422fd9134eb692d8f61db6b006e/href">https://medium.com/media/49d5b422fd9134eb692d8f61db6b006e/href</a></iframe><p>使用上：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83a4574af7d898f9c25bb3ee182dedd1/href">https://medium.com/media/83a4574af7d898f9c25bb3ee182dedd1/href</a></iframe><p>其實還蠻簡單的，torch tensor丟給Holder。剩下就是PyCUDA跟CUDA的使用，各位可以自己去看document。</p><p>解說一下</p><p>1. SourceModule的no_extern_c的意思，就是要他用C++模式去編譯，因為SDK附的helper_math.h是滿滿的C++</p><p>2. 我們自己的code去extern “C”，是因為c++ mangling會讓pycuda找不到ray_sphere這個symbol，因此我們放棄函數多載(function overloading)等C++特徵去export這個symbol，就可以用原始名稱找到這個函數。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29688ba26ea6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Numpy 實用小技巧]]></title>
            <link>https://tim37021.medium.com/numpy-%E5%AF%A6%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7-460ca6c177a2?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/460ca6c177a2</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[numpy]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Fri, 05 Apr 2019 16:26:14 GMT</pubDate>
            <atom:updated>2019-04-05T16:26:14.598Z</atom:updated>
            <content:encoded><![CDATA[<p>我們都知道python中，array的slice可以用下面來描述；</p><pre>arr[start:end:stride]</pre><p>1. stride在音訊處理上可以拿來做downsampling，圖片的話拿來做品質很差的mipmap。</p><pre>signal = signal[::2]</pre><p>2. 全選剩餘的所有dimension -&gt; arr[...]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea58de08aac39415d054070ada779989/href">https://medium.com/media/ea58de08aac39415d054070ada779989/href</a></iframe><p>3. 比較運算子</p><pre>arr = np.asarray([-1, 0, 1, -1, 2, 3])</pre><pre># [False, True, True, False, True, True]<br>arr!=-1</pre><p>4. np.where</p><p>np.where拿來整理出，比較後，結果是True的index是多少。筆者之前不知道有這個function的時候，為了加速幹了不少髒事情。不過後來發現有個numba可以做python jit，這改天再介紹</p><pre># [1, 2, 4]<br>np.where(arr!=-1)</pre><p>5. Array subscribe with array</p><p>這功能拿來多選(可以複選)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b2782cb121240936df877331c04550d/href">https://medium.com/media/1b2782cb121240936df877331c04550d/href</a></iframe><p>6. 指定運算子</p><p>要用指定運算子等號左右邊數量要一樣，或是右邊有一個list或tuple，dimension要跟左邊最後一個維度一樣。<strong>4跟5的技巧一起使用，常常拿來做thresholding</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80e3fdc258ed82a5c3a0feb78067b13a/href">https://medium.com/media/80e3fdc258ed82a5c3a0feb78067b13a/href</a></iframe><p>7. Structured array</p><p>關於Structured Array網路上很少有著墨，大概因為ML用不太到。這裡用我論文實作舉例，補充幾個用法。</p><p>假設我們要用描述一批射線，需要原點跟方向，他們各自都是R^3空間下。hits用來描述射線打到物體時的資訊，其中tri是三角形編號，沒打中的話t=-1.0, tri=-1</p><p>由於使用C Library去做加速，<strong>得確保little-endian</strong>，因此明確寫&lt;i4</p><pre>rays = np.zeros(shape=(600, 800), dtype=[(&#39;o&#39;, &#39;&lt;f4&#39;, 3), (&#39;d&#39;, &#39;&lt;f4&#39;, 3)])<br>hits = np.zeros(shape=(600, 800), dtype=[(&#39;t&#39;, &#39;&lt;f4&#39;), (&#39;tri&#39;, &#39;&lt;i4&#39;)])</pre><p>存取這些射線很簡單：</p><pre># 我覺得第一個方法比較合語意<br>rays[...][&#39;o&#39;]<br>rays[&#39;o&#39;]</pre><p>存取打中物體的射線</p><pre>active_rays = rays[hits[...][&#39;tri&#39;]!=-1]</pre><p>以光跡追蹤，我們要用這些active_rays繼續延伸射線</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95d20e87fcd71d2d552f7d7e8039e049/href">https://medium.com/media/95d20e87fcd71d2d552f7d7e8039e049/href</a></iframe><p>之後逆向跌代會用到這些active_indices，只是這超出本章要講述的範圍。有機會再說我怎麼用python寫path tracer</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=460ca6c177a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OptiX Prime 的使用(Python/C++)]]></title>
            <link>https://tim37021.medium.com/optix-prime-%E7%9A%84%E4%BD%BF%E7%94%A8-python-c-d5fe2ecaea78?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5fe2ecaea78</guid>
            <category><![CDATA[path-tracing]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[nvidia]]></category>
            <category><![CDATA[ray-tracing]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Fri, 05 Apr 2019 09:22:28 GMT</pubDate>
            <atom:updated>2019-04-05T09:22:28.184Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*JJz57gaAkSHnfAJF.png" /></figure><p>最近NVidia RTX顯卡很紅，我想原本的OptiX光跡追蹤引擎應該也因此被加速了不少。其中OptiX Prime是他的Low-level API，拿來讓你做triangle hit test</p><p>最近要實作論文就把OptiX Prime弄了個Python binding出來。為什麼是python？其實有在搞Graphics的人都知道：每次要寫ABCDEFG part你才看得到畫面，然而你只想寫C part，這實在浪費很多時間。所以我選擇用python來整合Library。</p><p>因為自動產生binding的code<strong>目前手動還是有點多</strong>。之後有空我會弄好程式碼給大家安裝使用。</p><p>其實prime是一個很簡單的library，有幾個大觀念各位需要知道：</p><ol><li>Context：就是你知道的Context，可以選擇要CUDA/CPU運算</li><li>BufferDesc：用來描述Buffer (長度/Stride/是GPU記憶體?)</li><li>Model：模型。通常由vertices跟indices組成(他可以讓你選)</li><li>Query：模型對一個批次的Ray做Hit Test，結果會存在Hit Buffer</li></ol><p>我寫的<strong>pyoptixprime</strong>用起來基本上跟C++ API沒啥兩樣，完全沒高階封裝，不用擔心要學兩套。</p><p>大概只要import這些就行了，不過pytorch或cupy都可以用(如果你想要最純粹的GPU加速)</p><pre>import pyoptixprime as prime<br>import numpy as np<br>#import torch<br>#import cupy as cp<br>ctx = prime.Context(prime.RTP_CONTEXT_TYPE_CUDA)</pre><p>這裡講比較複雜的BufferDesc，以建立Ray跟Hit為例子，然後再送去Query查詢，Query結束之後，答案會存在hits裡面，可以繼續用numpy操作</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8a6e2e78848790db44194cf131c13c6/href">https://medium.com/media/e8a6e2e78848790db44194cf131c13c6/href</a></iframe><p>可以看到BufferDesc用來描述自己指向的那個緩衝區的格式。對於Ray的origin/Dir大家應該沒什麼意見，但Hits只支援t跟三角形ID就說不過去了。當然這不可能!以下是支援的Format，基本上T_TRIID_U_V就很夠用了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*v5ldlclPrvZpOe03-DDOiQ.png" /><figcaption>Supported hit format</figcaption></figure><p>關於怎麼上傳Model也不是太困難</p><pre>#請自行準備好verts_desc indices_desc<br>mdl = ctx.createModel()<br># setTriangles2用來上傳 vertices/indices<br>mdl.setTriangles2(verts_desc, indices_desc)<br># 好了之後記得上傳模型<br># 這個操作可以讓你ASYNC(其實Query也可以)<br>mdl.update(prime.RTP_MODEL_HINT_ASYNC)<br># 確定上傳完畢可以呼叫finish<br>mdl.finish()</pre><p>至於怎麼傳CUDA Memory Pointer，你可以用Cupy或是Pytorch建立Tensor/array，然後用我提供的createBufferDesc2，這專門讓你傳指標</p><pre>ctx.createBufferDesc2(????, prime.RTP_BUFFER_TYPE_CUDA_LINEAR,    torch_tensor.data_ptr())</pre><p>放個圖以免大家以為這是假想的library。(對！你正在看一個還沒發布的library的說明書)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/0*e2e27_6VdT0j7SEm" /></figure><p>最後提供一個<a href="https://gist.github.com/tim37021/7b95481caf5f10620f8de64695ebe33c">Code</a>，示範如何整合tinyobjloader跟optix prime</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/0*fnrKGFAjpNZD9p00.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d46e7a4b3c963f8a5edec91377760c0/href">https://medium.com/media/6d46e7a4b3c963f8a5edec91377760c0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5fe2ecaea78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ultralight 嶄新的HTML UI Engine]]></title>
            <link>https://tim37021.medium.com/ultralight-%E5%B6%84%E6%96%B0%E7%9A%84html-ui-engine-87cad6827273?source=rss-90e1d5dd5600------2</link>
            <guid isPermaLink="false">https://medium.com/p/87cad6827273</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[許友綸]]></dc:creator>
            <pubDate>Fri, 05 Apr 2019 07:48:26 GMT</pubDate>
            <atom:updated>2019-04-05T07:48:26.351Z</atom:updated>
            <content:encoded><![CDATA[<p>現在大家對軟體的顏值要求不斷上升。因此，最近幾年一直使用Electron開發一些對UI/UX高要求的程式，比如SLiMTAB和ChessGameDesigner。</p><p>說老實話，Web-based UI真的讓排版變很簡單，尤其你家UI/UX人員還有機會學會這些東西，幫你弄出Layout。甚至還有很多動態UI套件可以直接使用。</p><p>我們來看第一個例子SLiMTAB：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*va5V3MhLEmk6ggswXpUBAw.png" /><figcaption>SLiMTAB — 樂譜編輯軟體。</figcaption></figure><p>中間樂譜就用SVG畫出來的！有夠方便，放大縮小功能天生支援，還能把樂譜匯出成PDF，這也是我用javascript寫出的第一支程式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*87c29kRuLTkWAYlK.png" /><figcaption>ChessGameDesigner — 棋類遊戲設計軟體</figcaption></figure><p>可是到了這個，可能我太習慣寫Graphics程式，Three.js用起來就很彆扭，做一些特別的事情還是得懂點Graphics API，有時候感覺自己寫比較快..</p><p>寫一寫，我就開始想：</p><ol><li>程式散亂一地，但程式太小根本不想用Framework</li><li>互動性太高的程式(Realtime)，也不一定適合用Framework</li><li>Typescript是我最喜歡的程式語言，但是遇到一些沒提供prototype的lib很頭痛，有時候會用很髒的方法支援</li><li>還是要把前後端放在心上，跟其他語言整合不太方便。</li><li>node_modules比本肥宅還胖</li></ol><p>恐怕我有強迫症，<strong>無法接受自己寫的程式體積遠小於使用的library</strong>，還有我也不怎麼會寫javascript。最近網路上看到一個很不錯的Project叫做<a href="https://ultralig.ht/">Ultralight</a>，他整個把UI邏輯分開了，只要實作自己的Renderer等元件就能嵌入到你的程式，是控制狂(Control Freak)最喜歡的那種函數庫。</p><p>下載SDK之後會發現這個東西異常的好用！弄一弄很快就能在自己的OpenGL+Imgui程式嵌入網頁了(爽拉。把網頁畫進貼圖顯示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*dhKREuzG0x_NMatnkMLuxA.png" /><figcaption>Stackoverflow in Imgui</figcaption></figure><p>與其說他是UI Engine，他像是完整的網頁引擎還支援Javascript。現階段有一些簡單的C++/Javascript交互可以用。</p><p>只不過要達成上面的嵌入有個點要注意 - <strong>Default Graphics Driver會用GLFW創OpenGL Context</strong>，直接照抄範例他會跟你的GLFW打架，再來就是Resource ID也不能互通(想Shared Context要改內部程式)，也就是畫好之後你得用他的Bitmap object讀出來再上傳到Context，很沒效率。也就是我們得實作自己的driver即便他原本就是用OpenGL...不過這東西抄一抄就好了</p><p>具體作法的虛擬碼如下：</p><ol><li>下載這份<a href="https://github.com/ultralight-ux/Ultralight/blob/master/deps/AppCore/src/linux/gl/GPUDriverGL.cpp">code</a>還有其對應header</li><li>準備流程如下</li></ol><pre>using namespace ultralight;<br>auto &amp;platform = Platform::instance();<br>GPUDriverGL driver(1.0f);</pre><pre>platform.set_config(ul::Config());<br>// 這裡放你的driver<br>platform.set_gpu_driver(&amp;driver);<br>platform.set_font_loader(ul::DefaultFontLoader());</pre><pre>renderer = Renderer::Create();<br>view = renderer-&gt;CreateView(800, 600, false);<br>view-&gt;LoadURL(&quot;https://stackoverflow.com&quot;);</pre><p>3. 繪圖流程</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*LfDBx3_g_c0Dz3WAPMsL_A.png" /></figure><p>就這麼簡單，至於互動功能再把輸入補進去即可!</p><p>至於如何跟JS互動，可以去看SDK裡面提供的Tutorial。有個Browser範例，連著UI一起做出來(TAB功能也是HTML+JS+CSS做的)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rlXbsanUWuWjEE3qDiGUKw.png" /><figcaption>Browser</figcaption></figure><p>我相信這麼好用的東西以後一定會有其他binding，pybind11應該能讓他很快支援python平台，如果有天python可以結合網頁UI就真的圓滿了!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87cad6827273" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>