2018年6月26日火曜日

Could not find com.android.tools.lint:lint-gradle:26.1.2 備忘録

Android Studio Gradle 系のトラブルが多い。

Could not find com.android.tools.lint:lint-gradle:26.1.2.

とかでエラーが出た場合は、プロジェクトの build.gradle を修正する

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
    }
}

allprojects {
    repositories {
        // google 先生が追加されますた
        google()
        jcenter()
    }
}

追記: jcenter と google の位置関係は、google を先にした方が良いようです。
gradle:3.1.3 と記述している箇所は、どんどんバージョンが上がります。

2018年6月9日土曜日

boost::compute テスト

ちょいと GPU 使ってみようかと思って OpenCL について調べました。macbook pro retina MID2012 が壊れて、GPU が Intel Iris しか搭載されてないものに買い替えたため、Cuda は選択肢に無くなったんです。データセンターで使うな騒動もあったし、Cuda は、もうええんですわ。
そしたら、boost::compute というのがあって、OpenCL を使えるらしいのです。
チュートリアルを macbook pro でコンパイルしてみたら動く感じだったんで、本当に速いのかチェックしてみました。

コンパイルは、brew install boost してある状態で、こんなん。
g++ tutorial3.cpp -I /usr/include -framework OpenCL -std=c++11 -lboost_timer
チュートリアルを少し弄ってあります。
#include <vector>
#include <algorithm>

#include <boost/compute/algorithm/transform.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/functional/math.hpp>

#include <iostream>
#include <cmath>
#include <boost/timer/timer.hpp>

namespace compute = boost::compute;

int main()
{
    // get default device and setup context
    compute::device device = compute::system::default_device();
    compute::context context(device);
    compute::command_queue queue(context, device);

    std::vector<float> host_vector(100000000);

    boost::timer::cpu_timer timer;

    float n = 1.0f;
    std::generate(host_vector.begin(), host_vector.end(), [&n]() { return n += 1.0f; });
    // generate random data on the host
    //std::generate(host_vector.begin(), host_vector.end(), rand);

    timer.start();

    // create a vector on the device
    compute::vector<float> device_vector(host_vector.size(), context);

    // transfer data from the host to the device
    compute::copy( host_vector.begin(), host_vector.end(), device_vector.begin(), queue );

    // calculate the square-root of each element in-place
    compute::transform(
        device_vector.begin(),
        device_vector.end(),
        device_vector.begin(),
        compute::sqrt<float>(),
        queue
    );

    // copy values back to the host
    compute::copy( device_vector.begin(), device_vector.end(), host_vector.begin(), queue );
    
    timer.stop();
    
    std::cout << "GPU: " << timer.format() << std::endl;

    n = 1.0f;
    std::generate(host_vector.begin(), host_vector.end(), [&n]() { return n += 1.0f; });
    
    std::sqrt<float>(n);
    
    timer.start();
    std::transform( host_vector.begin(), host_vector.end(), host_vector.begin(), static_cast<float (*)(float)>(std::sqrt));
    timer.stop();
    std::cout << "CPU: " << timer.format() << std::endl;
    
    //for( float x: host_vector) { std::cout << x << ","; }

    return 0;    
}
元は 10000 個の配列演算だったんですが、それだと
GPU:  0.078395s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
CPU:  0.000097s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)
メモリ転送の処理に時間がかかって効果を確認できず。
100000000 個の配列演算で比較すると
GPU:  0.367576s wall, 0.000000s user + 0.170000s system = 0.170000s CPU (46.2%)
CPU:  0.819997s wall, 0.820000s user + 0.000000s system = 0.820000s CPU (100.0%)
ようやく効果が確認できました。

追記: host_vector size が 1000000000 -> 100000000 に訂正しました

2018年6月4日月曜日

Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. 備忘録

どこかに書き留めないと、また調べる事になりそうなので、備忘録。

ある日突然、AndroidStudioでビルドをしようとすると、こんなメッセージが出てビルドできない。
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

はい、compile は deprecated になり、implementation コマンドに変更されました。
以下のように書き換える必要があります。
dependencies {
    //compile files('../../Ref/Java/android-support-v4.jar')
    //compile 'com.android.support:support-v4:18.0.0'
    implementation 'com.android.support:support-v4:18.0.0'

    //compile files('../../Ref/Java/foo.jar')
    implementation fileTree(dir: '../../Ref/Java', include: 'foo.jar')
    //compile files('../../Ref/Java/bar.jar')
    implementation fileTree(dir: '../../Ref/Java', include: 'bar.jar')
}