Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

IMF Device Library Usage Example

Intel Math Functions (IMF) Device Library power function calling example:

#include <sycl/ext/intel/math.hpp>

...

sycl::queue{}.submit([&](sycl::handler& h) {
    sycl::accessor out{a, h};
    h.parallel_for(r, [=](sycl::item<1> idx) {
      out[idx] = sycl::ext::intel::math::pow(x, y);
    });
  });

The pow.cpp example prompts you to enter two numbers, and then performs the power calculation using SYCL parallel computing.

The pow.cpp example:

/* file: pow.cpp */
/*******************************************************************************
 * Copyright 2024 Intel Corporation.
 *
 * This software and the related documents are Intel copyrighted  materials, and
 * your use of  them is  governed by the  express license  under which  they
 *were provided to you (License).  Unless the License provides otherwise, you
 *may not use, modify, copy, publish, distribute,  disclose or transmit this
 *software or the related documents without Intel's prior written permission.
 *
 * This software and the related documents  are provided as  is,  with no
 *express or implied  warranties,  other  than those  that are  expressly stated
 *in the License.
 *******************************************************************************/

#include <iostream>
#include <sycl/ext/intel/math.hpp>
#include <sycl/sycl.hpp>

/**
 * Number of inputs
 */
constexpr int num = 1;

/**
 * @brief Entry point of the program.
 *
 * This function calculates the power of two numbers using the SYCL framework.
 * It prompts user to enter two numbers, and then performs the power calculation
 * using SYCL parallel computing. The calculated results are printed to the console.
 * The expected result is also printed for comparison.
 *
 * @return 0 indicating successful execution of the program.
 */
int main() {
  float x, y;
  auto r = sycl::range{num};
  sycl::buffer<float> a{r};

  std::cout << "Enter x and y: ";
  std::cin >> x >> y;

  sycl::queue{}.submit([&](sycl::handler& h) {
    sycl::accessor out{a, h};
    h.parallel_for(r, [=](sycl::item<1> idx) {
      out[idx] = sycl::ext::intel::math::pow(x, y);
    });
  });

  sycl::host_accessor result{a};

  for (int i = 0; i < num; ++i) {
    std::cout << "Computed pow(" << x << ", " << y << ") = " << result[i]
              << "\n";
  }

  std::cout << "Expected pow(" << x << ", " << y << ") = " << std::pow(x, y)
            << "\n";

  return 0;
}

The calculated results are printed to your console. The expected reference result is also printed for comparison. Use the makefile to compile and run the simple test using the SYCL compiler set up in environment with:

make exe
make run
make clean

A makefile example:

# file: makefile
# ==============================================================================
# Copyright  (C) 2024 Intel Corporation.
#
# The information and source code contained herein is the exclusive property
# of Intel Corporation and may not be disclosed, examined, or
# reproduced in whole or in part without explicit written authorization from
# the Company.
# ==============================================================================

default: all
exe:     pow.exe
all:     run

SHELL := /bin/bash
	
pow.exe: pow.cpp
	icx -fsycl ./pow.cpp -o ./pow.exe

run: pow.exe
	./pow.exe

clean:
	@rm -f ./pow.exe