Saturday, March 8, 2014

ShadowRoot

I did not notice that dart:html includes some basic mechanism to register new HTML element type.
Since if we add internal variables associated to a new type of element, just a basic javascript type dom tree manipulation is not sufficient. dom to object invocation is required. this createShadowRoot mechanism looks useful feature indeed.
But ideally if Dart support nested class, I will define this MyElement (see below) as inner class of the Component class. Other approach is just to merge those features into single(MyElement) class, and this looks feasible way.
but if we want to use some of techniques using annotation for fields, we may need to define MirrorHtmlElement class as the subclass of HtmlElement, and 'component' class should inherit from it.
but there were some subtlety for the order of initialization, this approach need to be investigated further..
import 'dart:html';

void main() {
  document.register('my-element', MyElement);
  document.body.children.add(new Element.tag('my-element'));
}

class MyElement extends HtmlElement {

  MyElement.created() : super.created() {
    var shadowRoot = this.createShadowRoot();
    var button = new ButtonElement()..text = '0';
    var clickCount = 0;
    button.onClick.listen((e) {
      button.text = '${++clickCount}';
    });
    shadowRoot.children.add(button);
  }
}

The HTML is nearly empty except the necessary script imports:

<!DOCTYPE html>
<html>
  <body>
    <script type="application/dart" src="custom.dart"></script>
    <script src="packages/custom_element/custom-elements.debug.js"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

Parts based Programming vs Components based Programming

Dart is designed to support components based web programming.
But the meaning of 'component' looks different for each person.
For me, component is something like coarse grained composability, not fine grained reusability.
'component' would be best represented by 'component' stereo. We can combine any amplifier, preamplifier of different companies. just connecting by wires, it works.
Another example is PC mother board. We can combine any power supply, DRAM, HDD, Compact Disk, graphics board.
On the other hand, these components themselves uses parts, e.g, condensors, transisters(but these days, the amount of such parts are decreasing..).
What polymer seems trying to achieve is to provide such reusable 'part'.
Components should have bigger self consistent functionality, and should not expose the internal.
So the approach to design 'component' based framework and 'part' based framework will be different.

Such component system works because there is a fixed API/standard any component provider must follow. In programming language, that can be defined by interface.(in Dart this was strangely dropped, but we can still use abstract class).
To connect such providers and integrate functional(functorial) body, we can use generic class.
So if we use analogy, class corresponds 'part', and generic class corresponds to 'component'.

Monday, February 10, 2014

Cuda Sample Matrix performance (with 9800 GT) vs Java

I installed Cuda SDK 5.5, and ran one of sample Cuda Matrix program.
Then I tested two cases of matrix multiplication for 256 and 512 dimensions. (I actually tried to run 1012 case, but cuda could not handle that size.)

Since GPU computing is advertized to be very fast, like 100 time faster than CPU, I was expecting big difference, and did not expected it is slower than CPU!

Here is the result:
[Matrix Multiply Using CUDA] - Starting...
GPU Device 0: "GeForce 9800 GT" with compute capability 1.1

[CUDA]
256x256 matrix multiplication: 25.803 msec
512x512 matrix multiplication: 200.571 msec

[Single Thread Java]
dim: 256, elapsed time: 30 msec
dim: 512, elapsed time: 217 msec

[4 Threads Java(4 cores)]
dim: 256, elapsed time: 18.0 msec, dim^3; 16,777,216, ration(time/dim^3)=1.073
dim: 512, elapsed time: 105.0 msec, dim^3; 134,217,728, ration(time/dim^3)=0.782

From this result, we may say CUDA(with 9800GT) is similar performance with Single Thread Java.
and if we use 4 Threads Java version, CPU(intel I5) is twice faster than CUDA(9800GT).

9800GT is a bit old GPU, but if we compare with nVidia's top model GTX 780,  780 is about 4 times faster than 9800GT, so probably CUDA(GTX 780) will be twice faster than intel I5.

But, Intel is planning to release 8-core HASWELL-E processor in Q3/2014, so if we use this new CPU, the performance will become similar to CUDA(GTX780).

But it is not clear how large dimension GTX 780 can handle, if it is the same as 9800GT, CPU version is more powerful. 
Also CUDA requires unportable complex code for this 'optimized' GPGPU computing, Java version is much simpler, and also it may be possible to use 16 core with dual CPU motherboard, then that version will be twice faster than  CUDA(GTX780).

Bottom line is if the performance gain using CUDA(GPU) is such small difference, it does not make sense to use GPU.

-----
BTW, following is the my CPU spec. it is intel I5, 4 cores, 32GB RAM

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Stepping:              9
CPU MHz:               1600.000
BogoMIPS:              6935.22
Virtualization:        VT-x
L1d cache:             32K                                                                                                     
L1i cache:             32K                                                                                                     
L2 cache:              256K                                                                                                    
L3 cache:              6144K                                                                                                   
NUMA node0 CPU(s):     0-3                     
-----