Bin packing with first fit algorithm

Hi everybody,

I’m new on forum and I already have some troubles with CUDA programming. I was looking for first fit algorithm for bin packing resolve and I couldn’t find it so I decide to code it. I stuck with some errors and algorithm so could you help me or give me some oppinion.

I resolve algorithm in python and it works great, but I’m relatively new for CUDA. Thanks!

``````# First-fit algoritam - Rjesavanje problema pakiranja

import math
import sys
import binModule
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as ga
import numpy as np
from pycuda.compiler import SourceModule

print "\n\n"

# Korisnik unosi kapacitet spremnika
cap = binModule.getCap()

# Korisnik unosi elemente
items = binModule.getItems()

#inicijalizacija polja
length = len(items)
bins = np.zeros((2, length), dtype=np.float32)

print "Vasi elementi su:", items, "\nVasi spremnici imaju kapacitet ", cap, "\n"

#cijeli prvi redak polja postaje jednak kapacitetu
for x in range(0, len(items)):
bins[0][x] = cap

mod = SourceModule("""
#include <stdio.h>

__global__ void binPacking (float **bins, float *items, float cap, int length)
{
int y=0;
for(int i=0; i<length; i++){
int x=0;
if(items[i] < cap){
printf ("Element %f je veci od kapaciteta spremnika koji je \$f. PREKID!", items[i], cap);
break;
}
if(bins[0][x] >= items[i]){
bins[0][x] += items[i];
bins[1][x] -= items[i];
}
else{
while(bins[0][x] < items[i]){
x+=1;
}
bins[0][x] += items[i];
bins[1][x] -= items[i];
}
}
if(x>y)
y=x;
}
""")

binPacking = mod.get_function("binPacking")
binPacking(drv.Out(bins), drv.In(items),drv.In(cap), drv.In(length), block=(16,1,1), grid=(1,1))

print "First-fit algoritam za", items, "sa kapacitetom", cap, "koristio je", len(bins), "spremika"
print "Konfiguracija: ", bins
``````