oneAPI介绍及运行实例

oneAPI介绍

随着科学技术的飞速发展,高性能计算在人工智能、药物研制、智慧医疗、计算化学等领域发挥着日益重要的作用。随着后摩尔时代的到来,计算机系统结构进入了百花齐放百家争鸣的繁荣时期,CPU、GPU、FPGA和AI芯片等互为补充,硬件呈现出了更为多样性的现状。硬件的多样性带来了软件设计与开发的复杂性,人们迫切的需要能在不同平台、不同硬件架构上都能运行的编程模型。为解决此问题,Intel推出了oneAPI。

oneAPI中的工具包

oneAPI中有许多的工具包,以下介绍几种:
1.Intel® oneAPI Base Toolkit
这个工具包是 oneAPI 其他产品的基础,只有安装了它才可以继续安装和使用其他产品。包含了几个常用的Parallel Studio软件等。
2.Intel® oneAPI HPC Toolkit
这个工具包提供可扩展的快速C ++、Fortran、OpenMP和MPI应用程序。前两种工具包合并起来基本就包含Intel Parallel Studio XE的功能了。
3.Intel® oneAPI IoT Toolkit
这个工具包主要功能是,在构建高性能且安全可靠的解决方案的同时,能在网络边缘构建。可以联通网络和现实,多用于物联网领域。
4.Intel® AI Analytics Toolki
如其名,这个工具包提供AL方面的加速工具,主要是优化的深度学习框架和高性能Python库,加速端到端机器学习和数据科学库。最大化从预处理到机器学习的性能。
5.Intel® oneAPI Rendering Toolkit
这个工具包注重视觉体验。它主要用于创建高性能、高保真的可视化应用程序,适用于各种渲染工作。

运行实例

devCloud是一个免本地安装的给开发人员体验oneAPI开发工具的平台,在上面运行了包括矩阵相乘、向量相加的实例,效果拔群。在这里附上大神的代码:

#include <sycl/sycl.hpp>
#include <array>
#include <iostream>
#include <string>
#if FPGA_HARDWARE || FPGA_EMULATOR || FPGA_SIMULATOR
#include <sycl/ext/intel/fpga_extensions.hpp>
#endif

using namespace sycl;

// Array size for this example.
size_t array_size = 10000;

// Create an exception handler for asynchronous SYCL exceptions
static auto exception_handler = [](sycl::exception_list e_list) {
  for (std::exception_ptr const &e : e_list) {
    try {
      std::rethrow_exception(e);
    }
    catch (std::exception const &e) {
#if _DEBUG
      std::cout << "Failure" << std::endl;
#endif
      std::terminate();
    }
  }
};

//************************************
// Vector add in SYCL on device: returns sum in 4th parameter "sum".
//************************************
void VectorAdd(queue &q, const int *a, const int *b, int *sum, size_t size) {
  // Create the range object for the arrays.
  range<1> num_items{size};

  // Use parallel_for to run vector addition in parallel on device. This
  // executes the kernel.
  //    1st parameter is the number of work items.
  //    2nd parameter is the kernel, a lambda that specifies what to do per
  //    work item. the parameter of the lambda is the work item id.
  // SYCL supports unnamed lambda kernel by default.
  auto e = q.parallel_for(num_items, [=](auto i) { sum[i] = a[i] + b[i]; });

  // q.parallel_for() is an asynchronous call. SYCL runtime enqueues and runs
  // the kernel asynchronously. Wait for the asynchronous call to complete.
  e.wait();
}

//************************************
// Initialize the array from 0 to array_size - 1
//************************************
void InitializeArray(int *a, size_t size) {
  for (size_t i = 0; i < size; i++) a[i] = i;
}

//************************************
// Demonstrate vector add both in sequential on CPU and in parallel on device.
//************************************
int main(int argc, char* argv[]) {
  // Change array_size if it was passed as argument
  if (argc > 1) array_size = std::stoi(argv[1]);
  // Create device selector for the device of your interest.
#if FPGA_EMULATOR
  // Intel extension: FPGA emulator selector on systems without FPGA card.
  auto selector = sycl::ext::intel::fpga_emulator_selector_v;
#elif FPGA_SIMULATOR
  // Intel extension: FPGA simulator selector on systems without FPGA card.
  auto selector = sycl::ext::intel::fpga_simulator_selector_v;
#elif FPGA_HARDWARE
  // Intel extension: FPGA selector on systems with FPGA card.
  auto selector = sycl::ext::intel::fpga_selector_v;
#else
  // The default device selector will select the most performant device.
  auto selector = default_selector_v;
#endif

  try {
    queue q(selector, exception_handler);

    // Print out the device information used for the kernel code.
    std::cout << "Running on device: "
              << q.get_device().get_info<info::device::name>() << "\n";
    std::cout << "Vector size: " << array_size << "\n";

    // Create arrays with "array_size" to store input and output data. Allocate
    // unified shared memory so that both CPU and device can access them.
    int *a = malloc_shared<int>(array_size, q);
    int *b = malloc_shared<int>(array_size, q);
    int *sum_sequential = malloc_shared<int>(array_size, q);
    int *sum_parallel = malloc_shared<int>(array_size, q);

    if ((a == nullptr) || (b == nullptr) || (sum_sequential == nullptr) ||
        (sum_parallel == nullptr)) {
      if (a != nullptr) free(a, q);
      if (b != nullptr) free(b, q);
      if (sum_sequential != nullptr) free(sum_sequential, q);
      if (sum_parallel != nullptr) free(sum_parallel, q);

      std::cout << "Shared memory allocation failure.\n";
      return -1;
    }

    // Initialize input arrays with values from 0 to array_size - 1
    InitializeArray(a, array_size);
    InitializeArray(b, array_size);

    // Compute the sum of two arrays in sequential for validation.
    for (size_t i = 0; i < array_size; i++) sum_sequential[i] = a[i] + b[i];

    // Vector addition in SYCL.
    VectorAdd(q, a, b, sum_parallel, array_size);

    // Verify that the two arrays are equal.
    for (size_t i = 0; i < array_size; i++) {
      if (sum_parallel[i] != sum_sequential[i]) {
        std::cout << "Vector add failed on device.\n";
        return -1;
      }
    }

    int indices[]{0, 1, 2, (static_cast<int>(array_size) - 1)};
    constexpr size_t indices_size = sizeof(indices) / sizeof(int);

    // Print out the result of vector add.
    for (int i = 0; i < indices_size; i++) {
      int j = indices[i];
      if (i == indices_size - 1) std::cout << "...\n";
      std::cout << "[" << j << "]: " << j << " + " << j << " = "
                << sum_sequential[j] << "\n";
    }

    free(a, q);
    free(b, q);
    free(sum_sequential, q);
    free(sum_parallel, q);
  } catch (exception const &e) {
    std::cout << "An exception is caught while adding two vectors.\n";
    std::terminate();
  }

  std::cout << "Vector add successfully completed on device.\n";
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这里给你提供一个简单的代码实例,帮助你更好地理解如何利用VUE3+vite和kbone实现小程序与Web端同构。这个实例包含一个计数器组件,在小程序和Web端都可以正常运行。 首先,需要安装一些依赖包。在项目根目录下执行以下命令: ``` npm install --save kbone kbone-api @kbone/vue3-runtime npm install --save-dev vite @vue/compiler-sfc ``` 然后,在项目根目录下创建`src`文件夹,创建`main.ts`文件,输入以下代码: ``` import { createApp } from 'vue' import App from './App.vue' const app = createApp(App) app.mount('#app') ``` 这里使用Vue3创建了一个应用并挂载到`#app`元素上。 接着,创建`App.vue`文件,输入以下代码: ``` <template> <div> <div>{{ count }}</div> <button @click="increment">+</button> </div> </template> <script lang="ts"> import { defineComponent, reactive } from 'vue' export default defineComponent({ setup() { const state = reactive({ count: 0 }) const increment = () => { state.count++ } return { ...state, increment } } }) </script> ``` 这里定义了一个计数器组件,包含一个数字和一个按钮,点击按钮可以增加数字。 现在,我们需要将这个组件转换为小程序端的代码。在项目根目录下创建`src/miniprogram`文件夹,并创建`app.json`文件,输入以下代码: ``` { "pages": [ { "path": "pages/index/index", "style": { "navigationBarTitleText": "同构计数器" } } ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "同构计数器", "navigationBarTextStyle": "black" } } ``` 这里定义了小程序的页面路径和标题等信息。 接着,在`src/miniprogram`文件夹下创建`pages`文件夹,并在其中创建`index`文件夹,创建`index.ts`文件,输入以下代码: ``` import { createApp } from '@kbone/vue3-runtime' import App from '../../../App.vue' createApp(App).mount('#app') ``` 这里使用kbone的API将Vue3应用挂载到小程序的`#app`元素上。 最后,我们需要在项目根目录下创建`vite.config.ts`文件,输入以下代码: ``` import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false }, build: { minify: false, target: 'esnext', outDir: 'dist', emptyOutDir: true, assetsDir: 'static' } }) ``` 这里定义了vite的配置信息,包括插件、构建选项等。 现在我们可以在命令行中执行以下命令启动开发服务器: ``` npx vite --mode development ``` 在浏览器中访问`http://localhost:3000/`,可以看到计数器组件正常工作。 接着,我们需要将组件打包为小程序端的代码。在命令行中执行以下命令: ``` npx vite build --mode production ``` 这里使用vite的构建命令将组件打包为小程序端的代码。打包完成后,可以在`dist`文件夹下找到小程序端的代码。 将`dist/mp`文件夹中的文件复制到微信小程序的开发工具中,即可在小程序中看到同样的计数器组件。 以上就是一个简单的VUE3+vite利用kbone实现小程序与Web端同构的代码实例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值