January 22, 2009

Microsoft Accelerator (GPU) based Pi Calculation - A C# Port



This is a quick and dirty port.
Microsoft Research Accelerator Project


using
System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Research.DataParallelArrays;

namespace piSharp.cs
{
class Program
{
static void Main(string[] args)
{
Random r =
new Random();
// create randomly generated samples in the unit square centered on (0,0)

int iSize = 1500;
float[,] x = new float[iSize + 1, iSize + 1];
// x coordinate
float[,] y = new float[iSize + 1, iSize + 1];
// y coordinate
int i = 0;
int j = 0;

// create an x and y arrays of random numbers between 0 and 1
for (i = 0; i <= iSize - 1; i += 1) { for (j = 0; j <= iSize - 1; j += 1) { x[i, j] = (float)r.NextDouble();
y[i, j] = (
float)r.NextDouble();
}
}
ParallelArrays.InitGPU();

// make x and y data parallel arrays
DisposableFloatParallelArray parallelX =
new DisposableFloatParallelArray(x);
DisposableFloatParallelArray parallelY =
new DisposableFloatParallelArray(y);

// center them about (0, 0) with range (-1, 1)
FloatParallelArray parallelXCentered =
default(FloatParallelArray);
FloatParallelArray parallelYCentered =
default(FloatParallelArray);
parallelXCentered = ParallelArrays.Multiply(ParallelArrays.Subtract(parallelX, 0.5f), 2f);
parallelYCentered = ParallelArrays.Multiply(ParallelArrays.Subtract(parallelY, 0.5f), 2f);

// calculate distance of (x,y) from 0, ie. Sqrt(x^2 + y^2)
FloatParallelArray parallelXSquare =
default(FloatParallelArray);
FloatParallelArray parallelYSquare =
default(FloatParallelArray);
FloatParallelArray parallelDistance =
default(FloatParallelArray);
parallelXSquare = ParallelArrays.Multiply(parallelXCentered, parallelXCentered);
parallelYSquare = ParallelArrays.Multiply(parallelYCentered, parallelYCentered);
parallelDistance = ParallelArrays.Sqrt(ParallelArrays.Add(parallelXSquare, parallelYSquare));
float[,] test = new float[iSize + 1, iSize + 1];
ParallelArrays.ToArray(parallelDistance,
out test);

// create an array of 1's if distance <> 1
FloatParallelArray parallelOne =
new FloatParallelArray(1f, parallelX.Shape);
FloatParallelArray parallelZero =
new FloatParallelArray(0f, parallelX.Shape);
FloatParallelArray parallelInCircle =
default(FloatParallelArray);

parallelInCircle = ParallelArrays.Select(ParallelArrays.Subtract(parallelOne, parallelDistance), parallelOne, parallelZero);

// the number inside the circle is the sum of the entire InCircle array
FloatParallelArray parallelCountInCircle =
default(FloatParallelArray);
parallelCountInCircle = ParallelArrays.Sum(parallelInCircle);

float[] inCircle = new float[2];
ParallelArrays.ToArray(parallelCountInCircle,
out inCircle);
parallelX.Dispose();
parallelY.Dispose();

// approximate pi--area of unit circle is pi
// area of square from -1,1 is 4
// inCircle/(total number of points) == pi/4
float pi = 0;
pi = 4 * inCircle[0] / (iSize * iSize);
System.Console.WriteLine(
"Pi is approximately " + pi.ToString());
ParallelArrays.UnInit();

// Prompt user for exit for running in VS IDE
System.Console.WriteLine(
"Press Enter to Exit");
System.Console.ReadLine();
}
}
}


Back to Home

September 29, 2008

AWLP And AWAPI - New versions out, now!

Lately I had a lot of spare time to work on my projects , and now I'm proud to announce new versions of Animaonline Weather API (2.0.3.0 BETA) and Animaonline Windows Live Presence Wrapper (2.0)


Animaonline Weather API 2.0.3.0 BETA
What's new:

The GPS Code was improved and tested on a Windows Mobile Device, it works now :)
A Windows Mobile GPS Test Application was added.
Note: GPS code is still unstable
Image Downloader for Windows Mobile

AWLP 2.0 (Animaonline Windows Live Presence Wrapper)
For those who don't know what AWLP is, here is a short description:
Animaonline Windows Live Presence Wrapper Let's you query Windows Live service for user's status.
Powered by Microsoft's Windows Live API
Usage Example:
Animaonline.WindowsLive.PresenceData Presence;
Presence.GetPresence("animaonline@gmail.com");
Console.WriteLine(Presence)

Returns user's current status / status text , nickname and icon

What's new:
The code has been improved in many ways, and the assembly is now CLS compliant :)


Back to Home

September 21, 2008

Animaonline Weather API 2.0 BETA, Released



Okay! It's been a while since I last posted, a lot has happened lately
and I simply didn't have time to post, but now I'm back,
and I would like to announce the release of Animaonline Weather API 2.0 BETA,
quite some time has passed since the first release (One year actually!), I had some time to go trough the code, and it was then that I found out that the whole code would be better if I rewrite it from scratch; so I did..

The new release of awAPI has A LOT of improvements.

  • The performance has been drastically improved by 70-100 percent
  • The user now has ability to select the language the weather data will be queried in.
  • Querying weather data is now even simpler, can be done by one line of code!
  • I added GeoServices support, this includes GeoCoding (Which lets you find associated geographic coordinates from other geographic data, such as street addresses, or zip codes)
  • Reverse GeoCoding (Which is the opposite of GeoCoding: finding an associated textual location such as a street address, from geographic coordinates.)
  • GPS Support (Combined with GeoServices let's you query weather for the region you are in)
  • Google’s Maps support, allows you to generate Google Maps combining it with weather, GeoServices and GPS data
  • Multi Platform support, run the API on Windows, CE Devices, and the Mono Framework

Note: This is still a BETA version; I'm working on improving the final version and adding new features.

Check out the Recent Check-ins page for the latest version or visit this page for a compiled binary.

P.S.
You can make a donation if you'd like to support my work.


Back to Home

March 06, 2008

Internet Explorer 8 BETA





First Look: Internet Explorer 8

Here are some end-user features you can expect to see in Internet Explorer 8 Beta 1.


Activities
Activities are contextual services to quickly access a service from any webpage, like Facebook or Digg, Live Search, etc...

WebSlices
WebSlices is a new feature for websites to connect to their users by subscribing to content directly within a webpage. WebSlices behave just like feeds where clients can subscribe to get updates and notify the user of changes.

Favorites Bar
Like the one in Mozilla Firefox, lets you drag and drop URL's on it.

Automatic Crash Recovery

This one helps to prevent the loss of work and productivity in the unlikely event of the browser crashing or hanging.

Improved Phishing Filter
Internet Explorer 7 introduced the Phishing Filter, a feature which helps warn users when they visit a Phishing site. Phishing sites spoof a trusted legitimate site, with the goal of stealing the user’s personal or financial information. For Internet Explorer 8, we are building on the success of the Phishing Filter with a more comprehensive feature called the "Safety Filter."

Download it here

What’s new in Internet Explorer 8 for Developers
  1. Accessibility
  2. ActiveX Improvements
  3. Activities and WebSlices
  4. AJAX Enhancements
  5. CSS Compliance
  6. Developer Tools
  7. Document Compatibility Mode
  8. DOM Storage
  9. Protected Mode Cookies
  10. Selectors API
  11. Tab Isolation and Concurrency
For a detailed list go to this page


Back to Home

December 11, 2007

Windows Powershell 2.0 CTP Available



Microsoft has released Windows Powershell 2.0 CTP to Microsoft Beta testers, you can apply for the beta program at https://connect.microsoft.com/

A lot of new features has been added, here are my favorites

  • Script Debugger (Very neat)
  • GUI !
For a full list of new features go to Powershell 2.0 CTP beta program site at Microsoft Connect


Back to Home

December 07, 2007

Vista SP1 RC is here

Microsoft Released Vista SP1 to MSDN subscribers, here is the script to make it Visible in Windows Update, Right now I'm installing it, I will report as soon as the installation has been completed.

- Enjoy


Note: As far as I know the script should work on any Windows Vista versions, and in all countries.
How to update:

Open notepad -> Paste following text
@echo off
reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\VistaSp1 /f > NUL 2>&1
reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\WindowsUpdate\VistaSP1 /f > NUL 2>&1
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\VistaSp1 /v Beta1 /t REG_SZ /d da2ba4db-dedb-437e-8e7e-104643454bb6 /f
IF NOT %errorlevel% == 0 ( goto ERROR)
:SUCCESS
goto END
:ERROR
goto END
:END
pause
Save as .BAT or .CMD
Run as administrator (Right click the file, and click "Run as administrator")
Now check for updates on Windows Update


Back to Home

November 27, 2007

CodeDom Calculator


How to use
1: Compile the class and add to your application reference list.

2: Add the following line to display output
Console.WriteLine(CodeDomCalc.Calculator.Execute("2*55/1.5", "double").ToString());
[Save]


Back to Home

November 20, 2007

Visual Studio 2008 RTM avaialble for download


MSDN subscribers can now download Visual Studio 2008 RTM from MSDN


Back to Home

November 14, 2007

Converting Excel's DateTime serial to Windows DateTime



This little snippet here will allow you to convert excel encoded date to standard Windows DateTime.

public static DateTime ExcelDateConverter(int excelDateSerial)
{
if (excelDateSerial <>
{
excelDateSerial++;
}
return new DateTime((excelDateSerial + 693593) * (10000000L * 24 * 3600));

}


Back to Home

November 12, 2007

F# Windows Forms Tutorial



Here's a little tutorial on how to show a windows form from F# (The new .NET Language)

You can get the compiler from here


open System
open System.Windows.Forms

let form = new Form()
do form.Width <- 300
do form.Height <- 300
do form.FormBorderStyle <- FormBorderStyle.Fixed3D
do form.Text <- "Windows Form from F#"

let RichTextbox1 = new RichTextBox()
do RichTextbox1.Text <- "Hello Windows Forms From F#"
do RichTextbox1.Size <- new System.Drawing.Size(320,240)

let btn1 = new Button()
do btn1.Text <- "Click me!"
do btn1.Location <- new System.Drawing.Point(0,240)
let btn1Click (e: #IEvent<_>) = e.Add(fun _ -> System.Windows.Forms.MessageBox.Show("Hello World, from btnClick event handler!");())

do btn1Click btn1.Click

do form.Controls.Add(RichTextbox1)
do form.Controls.Add(btn1)

do Application.Run(form)


Back to Home

November 07, 2007

Office application does not quit after automation from Visual Studio .NET client


This is a common nasty problem that has bugged me a lot lately

When you automate a Microsoft Office application from Microsoft Visual Basic .NET or Microsoft Visual C# .NET, the Office application does not quit when you call the Quit method.

Here's the solution:

In your form's FormClosing event add the following code

ExcelApp.Application.Application.Quit();
ExcelApp.Application.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();


Source: http://support.microsoft.com/?kbid=317109


Back to Home

September 23, 2007

Fixing "Cross-thread operation not valid"


Did you ever get this error? Well I did! I got it pretty often lately…

But I found a quick fix that you can use to fix that issue instead of writing a lot of thread handling code, joining the threads, and so on…

You can just use this little snippet in your application’s Main void

Here it goes:
CheckForIllegalCrossThreadCalls = false;

P.S. Remember to add System.Windows.Forms to your assembly!


Back to Home

September 18, 2007

Geek Test

I took the same test as Josh did and here I get exactly the same results, :D
code geek
But there's a couple of things that are not correct here, First of all; I don't use *nix OS’es, and no, I did not write my first program in the sixth grade, (I guess it was in the kindergarten)


Back to Home

September 17, 2007

Vana'diel Time in C# (Vana'diel Crazy Taru Clock) ヴァナ・ディール ☆基地外 たる 時計☆

ヴァナ・ディール ☆基地外 たる 時計☆
Vana'diel Crazy Taru Clock

I was bored , so decided to port the Pyogenes "Vana'diel Time" Clock to C#
About:
Vana'diel time is 25 times faster than Earth time and is being used in the fictional? universe of Final Fantasy XI Online


I made a simple API that you can use to get current weather in Vana'diel, that means you can use it in any application you want, everything from Command Line Applications, to AJAX Powered ASP.NET websites, remember, you have to use Timer.Tick() to update the information from the API! Or you can implement it directly to the class, if you want to; do whatever you want with it, just remember to credit me and Pyogenes for his great formula.

Note this is the first implementation of Vana'diel clock in C# , if I'm wrong , someone correct me...

Here goes the code (Sorry, no comments (yet), I'm sleepy):

using System;
using System.Collections.Generic;
using System.Text;
namespace Animaonline.Vanadiel
{
public static class Time
{
private static DateTime basisDate = new DateTime(2002, 06, 23, 15, 00, 00, 0000);
private static long msRealDay = 86400000;
private static string[] VanaDay = { "Firesday", "Earthsday", "Watersday", "Windsday", "Iceday", "Lightningday", "Lightsday", "Darksday" };
private static double vanaDate
{
get { return ((898 * 360 + 30) * msRealDay) + ((DateTime.UtcNow - basisDate).TotalMilliseconds * 25); }
}
public static double vYear
{
get { return Math.Floor(vanaDate / (360 * msRealDay)); }
}
public static double vMon
{
get { return Math.Floor((vanaDate % (360 * msRealDay)) / (30 * msRealDay)) + 1; }
}
public static double vDate
{
get { return Math.Floor((vanaDate % (30 * msRealDay)) / (msRealDay)) + 1; }
}
public static double vHour
{
get { return Math.Floor((vanaDate % (msRealDay)) / (60 * 60 * 1000)); }
}
public static double vMin
{
get { return Math.Floor((vanaDate % (60 * 60 * 1000)) / (60 * 1000)); }
}
public static double vSec
{
get { return Math.Floor((vanaDate % (60 * 1000)) / 1000); }
}
public static double vDay
{
get { return Math.Floor((vanaDate % (8 * msRealDay)) / (msRealDay)); }
}
public static string TextTime = vMon + "/" + vDate + "/" + vYear + " (" + VanaDay[Convert.ToInt16(vDay)] + ") " + vHour + ":" + vMin + ":" + vSec;
}
}

Usage: To use the code in your application just create a new class and paste the code (you can also compile it into a dll, and share it with your friends, if you want to)

In your application's main void or click event or whatever write the following code:

MessageBox.Show(Animaonline.Vanadiel.Time.vHour + ":" + Animaonline.Vanadiel.Time.vMin + ":" + Animaonline.Vanadiel.Time.vSec);

That's it, enjoy! :D


Back to Home

September 16, 2007

New sleek look, and Digg support

I refurbished the look and feel of the blog, making it more code article friendly
(Yes you can expect a lot of them, in the near future)
I also added Digg support, so you can just start Digg’in along.


Back to Home