Write a program or function that:

  1. Takes as input a list or array of 128 numbers.

  2. Sends that array to a GPU

  3. Performs a calculation on the GPU to square each element of the array and then sum them

  4. Gets the answer from the GPU, and returns or displays it

So your program will simply return the sum of the squares of its input array, but it will perform the calculation on a GPU.

The input numbers should be sent to the GPU in the form of signed or unsigned integer types or floats, of 32 or more bits in size.

You may make any assumptions you like about hardware, e.g. you may assume a CUDA or OpenCl capable card is present without checking for it.

This is . The smallest code (in bytes) will be considered the winner for each language.

  • \$\begingroup\$ Sandbox (deleted) \$\endgroup\$
    – N. Virgo
    Commented Mar 9, 2018 at 0:41
  • 1
    \$\begingroup\$ that would be +/×⍨ in apl with github.com/Co-dfns/Co-dfns :) \$\endgroup\$
    – ngn
    Commented Mar 9, 2018 at 0:48
  • \$\begingroup\$ @ngn I'm curious why you didn't post that as an answer - is there a reason? \$\endgroup\$
    – N. Virgo
    Commented Mar 9, 2018 at 0:54
  • 1
    \$\begingroup\$ ...and there's always danger that the co-dfns compiler might decide that using the gpu is not worth it for arrays of size <1000000 :) - I haven't been following the trends there for a while \$\endgroup\$
    – ngn
    Commented Mar 9, 2018 at 1:05
  • 1
    \$\begingroup\$ @ngn Your 10-year-old laptop has a discreet GPU? \$\endgroup\$
    – Adám
    Commented Mar 9, 2018 at 1:11

2 Answers 2


Processing.org 392 bytes

392 counting the shader and function

It's possible to reduce the number of characters by putting the shader program in a separate file.

String[] f={"uniform float x[128];void main(){float s;for(int i=0;i<128;i++)s+=x[i]*x[i];int b=floatBitsToInt(s);gl_FragColor=vec4(b>>16&255,b>>8&255,b&255,b>>24&255)/255.;}"},v={"uniform mat4 transform;in vec4 vertex;void main(){gl_Position=transform*vertex;}"};PShader p;void s(float[]i){p=new PShader(this,v,f);p.set("x",i,1);filter(p);loadPixels();print(Float.intBitsToFloat(pixels[0]));}
void setup(){
float[] input = new float[128];
for(int i=0;i<128;i++)input[i]=(float)i;
s(input); // prints 690880.0

APL (Dyalog Unicode) with Co-dfns, 7 bytesSBCS

Anonymous prefix lambda. I have verified with the author of Co-dfns that the code will indeed run according to the required spec.


Try it online! (equivalent code, but TIO doesn't provide GPU access)

{} compilable dfn; is the argument vector

+.× dot-product of

 itself ("selfie")

 using the argument as data


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.