My DPC++ code doesn't doesn't compile as the loops I stated below why?

190 views Asked by At

Hello there I am trying to parallelize my two loops using DPC++ but it gives an LLVM ERROR: SPIRV internal error. I don't know why, could you guys help me figure out the problem?

https://simplecore-ger.intel.com/techdecoded/wp-content/uploads/sites/11/Webinar-Slides-DPC-Part-1-An-Introduction-to-the-New-Programming-Model-.pdf

https://simplecore-ger.intel.com/techdecoded/wp-content/uploads/sites/11/Webinar-Slides-DPC-Part-2-Programming-Best-Practices.pdf

char* rsa_decrypt_without_private_key(const long long* message, const unsigned long message_size, const struct public_key_class* pub)
{
    struct private_key_class unknownpriv[1];
    long long p, q, phi_max, e, d;
    p = public_prime;
    q = pub->modulus / p;
    phi_max = (p - 1) * (q - 1);
    e = powl(2, 8) + 1;
    d = ExtEuclid(phi_max, e);
    while (d < 0) {
        d = d + phi_max;
    }
    unknownpriv->modulus = pub->modulus;
    unknownpriv->exponent = d;

    if (message_size % sizeof(long long) != 0) {
        fprintf(stderr, "Error: message_size is not divisible by %d, so cannot be output of rsa_encrypt\n", (int)sizeof(long long));
        return NULL;
    }
    
    char* decrypted = (char*) malloc(message_size / sizeof(long long));
    
    char* temp = (char*) malloc(message_size);
    if ((decrypted == NULL) || (temp == NULL)) {
        fprintf(stderr, "Error: Heap allocation failed.\n");
        return NULL;
    }

    buffer<char, 1> A(decrypted, range<1>{ message_size / sizeof(long long)});
    buffer<char, 1> B(temp, range<1>{ message_size });
    auto R = range<1>{ message_size / 8 };
    queue z;

    z.submit([&](handler& h) {
        auto out = B.get_access<access::mode::write>(h);
        h.parallel_for(R, [=](id<1> i) {
            out[i] = rsa_modExp(message[i], unknownpriv->exponent, unknownpriv->modulus);
        });
    });


    z.submit([&](handler& h) {
        auto out = A.get_access<access::mode::write>(h);
        auto in = B.get_access<access::mode::read>(h);
        h.parallel_for(R, [=](id<1> i) {
            out[i] = in[i];
        });
    });
    
    /*
    for (i = 0; i < message_size / 8; i++) {
        temp[i] = rsa_modExp(message[i], unknownpriv->exponent, unknownpriv->modulus);
    }

    for (i = 0; i < message_size / 8; i++) {
        decrypted[i] = temp[i];
    }*/

    free(temp);
    return decrypted;
}
1

There are 1 answers

0
Rahul_Intel On

As you are trying to call a function(rsa_modExp) inside the parallel_for kernel, you need to make sure that the function is present in the same translation unit.

When we want to call functions inside a kernel that are defined in a different translational unit, those functions need to be labeled with SYCL_EXTERNAL. Without this attribute, the compiler will only compile a function for use outside of device code (making it illegal to call that external function from within device code).

Link to the DPC++ book for further reference: https://www.apress.com/us/book/9781484255735 (Chapter 13)