Android Studio 3.2.1上vuh库使用的例子

栏目: IOS · Android · 发布时间: 6年前

内容简介:工程中几处关键的代码如下:点击此处下载完整的工程vuhAndroid

Android Studio 3.2.1vuh 库使用的例子,首先使用 基于Vulkan的GPGPU计算框架Vuh 编译出 Android 版本的动态库,然后依照如下步骤建立工程。

Android Studio 3.2.1上vuh库使用的例子 Android Studio 3.2.1上vuh库使用的例子

Android Studio 3.2.1上vuh库使用的例子 Android Studio 3.2.1上vuh库使用的例子 Android Studio 3.2.1上vuh库使用的例子

Android Studio 3.2.1上vuh库使用的例子

Android Studio 3.2.1上vuh库使用的例子

如下图所示,拷贝头文件跟动态库到指定的目录:

Android Studio 3.2.1上vuh库使用的例子

工程中几处关键的代码如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
 
# Sets the minimum version of CMake required to build the native library.
 
cmake_minimum_required(VERSION 3.4.1)
 
 
# for Vulkan
add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR=1 -DVULKAN_HPP_TYPESAFE_CONVERSION=1)
 
include_directories(src/main/cpp/include)
include_directories(${ANDROID_NDK}/sources/third_party/vulkan/src/include/)
 
add_library(vuh SHARED IMPORTED)
set_target_properties(vuh PROPERTIES IMPORTED_LOCATION  ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libvuh.so)
 
add_library(vulkan SHARED IMPORTED)
set_target_properties(vulkan PROPERTIES IMPORTED_LOCATION  ${ANDROID_NDK}/platforms/${ANDROID_PLATFORM}/arch-arm/usr/lib/libvulkan.so)
 
 
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
 
add_library( # Sets the name of the library.
        native-lib
 
        # Sets the library as a shared library.
        SHARED
 
        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)
 
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
 
find_library( # Sets the name of the path variable.
        log-lib
 
        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log
        android)
 
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
 
target_link_libraries( # Specifies the target library.
        native-lib
 
        vulkan
        vuh
        # Links the target library to the log library
        # included in the NDK.
 
        ${log-lib})
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.mobibrw.vuhandroid"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14 -v -g"
                abiFilters "armeabi-v7a"
            }
        }
    }
 
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
package com.mobibrw.vuhandroid;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("vuh");
        System.loadLibrary("native-lib");
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }
 
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
#include <jni.h>
#include <string>
 
#include <vuh/array.hpp>
#include <vuh/vuh.h>
#include <vector>
 
auto saxpy()-> int {
    auto y = std::vector<float>(128, 1.0f);
    auto x = std::vector<float>(128, 2.0f);
    const auto a = 0.1f; // saxpy scaling constant
 
    auto instance = vuh::Instance();
    auto device = instance.devices().at(0);  // just get the first compute-capable device
 
    auto d_y = vuh::Array<float>(device, y); // allocate memory on device and copy data from host
    auto d_x = vuh::Array<float>(device, x); // same for x
 
    using Specs = vuh::typelist<uint32_t>;
    struct Params{uint32_t size; float a;};
    auto program = vuh::Program<Specs, Params>(device, "saxpy.spv"); // define the kernel by linking interface and spir-v implementation
    program.grid(128/64).spec(64)({128, a}, d_y, d_x); // run once, wait for completion
    d_y.toHost(begin(y));                              // copy data back to host
 
    return 0;
}
 
 
extern "C" JNIEXPORT jstring JNICALL
Java_com_mobibrw_vuhandroid_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    saxpy();
    return env->NewStringUTF(hello.c_str());
}

点击此处下载完整的工程vuhAndroid

参考链接


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

区块链与人工智能:数字经济新时代

区块链与人工智能:数字经济新时代

高航、俞学劢、王毛路 / 电子工业出版社 / 2018-7-23 / 80

《区块链与人工智能》是畅销书《区块链与新经济:数字货币2.0时代》全新修订升级版。本书是市场上为数不多的系统阐述区块链、人工智能技术与产业的入门级系统教程。从比特币到各类数字货币(代币),从基础原理到应用探讨,全景式呈现区块链与人工智能的发展脉络,既有历史的厚重感也有科技的未来感。本书的另一个亮点是系统整理了区块链创业地图,是一本关于区块链创业、应用、媒体的学习指南,以太坊创始人Vitalik专门......一起来看看 《区块链与人工智能:数字经济新时代》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具