mardi 26 avril 2016

Unexpected results when calling cublas in C++/CLI and C#

I have written a wrapper in C++11/CLI with Visual Studio to use CUDA's CuBLAS. I am using CUDA Toolkit 7.0.

Here is the source code of my wrapper:

#pragma once

#include "stdafx.h"
#include "BLAS.h"
#include "cuBLAS.h"

namespace lab
{
    namespace Mathematics
    {
        namespace CUDA
        {

            void BLAS::DAXPY(int n, double alpha, const array<double> ^x, int incx, array<double> ^y, int incy)
            {
                pin_ptr<double> xPtr = &(x[0]);
                pin_ptr<double> yPtr = &(y[0]);
                pin_ptr<double> alphaPtr = &alpha;

                cuBLAS::DAXPY(n, alphaPtr, xPtr, incx, yPtr, incy);
            }
       }
   }
}

To test this code, I wrote the following test in C#:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using lab.Mathematics.CUDA;

namespace lab.Mathematics.CUDA.Test
{
  [TestClass]
  public class TestBLAS
  {
    [TestMethod]
    public void TestDAXPY()
    {
        var count = 10;
        var alpha = 1.0;
        var a = Enumerable.Range(0, count).Select(x => Convert.ToDouble(x)).ToArray();
        var b = Enumerable.Range(0, count).Select(x => Convert.ToDouble(x)).ToArray();

        // Call CUDA
        BLAS.DAXPY(count, alpha, a, 1, b, 1);

        // Validate results
        for (int i = 0; i < count; i++)
        {
            Assert.AreEqual(i + i, b[i]);
        }
    }
  }
}

The program compiles with x64 architecture with no error. But the results I get are different every time I run the test. More precisely, the array b is the result and it has different values every time. And I don't know why.

Aucun commentaire:

Enregistrer un commentaire