multithreading - Difference in map operation between parallel and sequential Scala collections -
i coding little scala snippet test difference in performance of parallel , procedural scala collections on machine using personal java image processing code refactoring. however, encountered strange issue use of map on parallel collection (pararray in case): calculation doesn't seem kick off (the program doesn't halt neither enter getbrightness def). seems enter blocking state.
here code :
def getbrightness(bright: array[float], kernel : array[array[float]], kernelsize : int, normfactor : float, x : int, y : int) : float = { var brightness : float = 0 for(dx <- -(kernelsize / 2) kernelsize / 2){ for(dy <- -(kernelsize / 2) kernelsize / 2){ val kerval = kernel(kernelsize / 2 + dx)(kernelsize / 2 + dy) brightness += bright(index(dx + x, dy + y)) * kerval } } brightness / normfactor } def convolute(pixels : pararray[color], kernel : array[array[float]]) : pararray[color] = { val bright : array[float] = pixels.map(c => color.rgbtohsb(c.getred, c.getblue, c.getgreen, null)(2)).toarray val normfactor : float= kernel.tolist.flatten.sum pixels.zipwithindex .map{case(_ , idx) => getbrightness(bright, kernel, kernel.length, normfactor, idx % imgwidth, idx / imgwidth)} .map(color.gethsbcolor(0, 0, _)) }
it issue understanding of parallel collection as, if change collection sequential array :
pixels.zipwithindex .seq //switching sequential collection .map{case(_ , idx) => getbrightness(bright, kernel, kernel.length, normfactor, idx % imgwidth, idx / imgwidth)} .map(color.gethsbcolor(0, 0, _)) .par }
then goes fine , convolution performed. know problem comes getbrightness call inside map debugger loop on , never execute def itself, i.e. never enters getbrightness definition. come accesses on bright array, causing blocking state ? using in read-only fashion, believe thread safe. little bit confused...
Comments
Post a Comment