Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/vowpal_wabbit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorU-NORTHAMERICA\jcl <jcl@JCL.northamerica.corp.microsoft.com>2014-07-11 23:53:54 +0400
committerU-NORTHAMERICA\jcl <jcl@JCL.northamerica.corp.microsoft.com>2014-07-11 23:53:54 +0400
commitc552fc7db1bd80d0c1aba52e292a92506b8ff1b8 (patch)
treea57c738b696b110220723f803f95c3ffc533c5aa
parentce359a281a64c0745911931a75b6b503fbac64ac (diff)
new dll interface which avoids various brokenness
-rw-r--r--cs_test/Program.cs32
-rw-r--r--cs_test/VowpalWabbitInterface.cs235
2 files changed, 143 insertions, 124 deletions
diff --git a/cs_test/Program.cs b/cs_test/Program.cs
index 7660e8f2..499ed9f1 100644
--- a/cs_test/Program.cs
+++ b/cs_test/Program.cs
@@ -68,7 +68,7 @@ namespace cs_test
tfeatures[2].x = 1;
featureSpace[1].len = 3;
- IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, featureSpace.Length);
+ IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, (IntPtr)featureSpace.Length);
VowpalWabbitInterface.AddLabel(importedExample, 1);
@@ -97,13 +97,13 @@ namespace cs_test
float label = VowpalWabbitInterface.GetLabel(example);
count++;
- int featureSpaceLen = 0;
+ IntPtr featureSpaceLen = (IntPtr)0;
IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, example, ref featureSpaceLen);
- VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
+ VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[(int)featureSpaceLen];
int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
- for (int i = 0; i < featureSpaceLen; i++)
+ for (int i = 0; i < (int)featureSpaceLen; i++)
{
IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
@@ -167,7 +167,7 @@ namespace cs_test
VowpalWabbitInterface.StartParser(vw, false);
- uint stride = VowpalWabbitInterface.Get_Stride(vw);
+ uint stride = (uint)VowpalWabbitInterface.Get_Stride(vw);
int count = 0;
IntPtr example = IntPtr.Zero;
@@ -180,22 +180,22 @@ namespace cs_test
float initial = VowpalWabbitInterface.GetInitial(example);
float label = VowpalWabbitInterface.GetLabel(example);
- UInt32 tag_len = VowpalWabbitInterface.GetTagLength(example);
+ UInt32 tag_len = (UInt32)VowpalWabbitInterface.GetTagLength(example);
byte[] tag = new byte[tag_len];
if (tag_len > 0)
- //Marshal.Copy(VowpalWabbitInterface.GetTag(example), tag, 0, (int)tag_len); //error CS1502: The best overloaded method match for 'System.Runtime.InteropServices.Marshal.Copy(int[], int, System.IntPtr, int)' has some invalid arguments
- ;
- UInt32 num_features = VowpalWabbitInterface.GetFeatureNumber(example);
+ Marshal.Copy(VowpalWabbitInterface.GetTag(example), tag, 0, (int)tag_len);
+
+ UInt32 num_features = (UInt32)VowpalWabbitInterface.GetFeatureNumber(example);
VowpalWabbitInterface.FEATURE[] f;
if (num_features > 0)
{
f = new VowpalWabbitInterface.FEATURE[num_features];
- int feature_count = 0;
+ IntPtr feature_count = (IntPtr)0;
IntPtr ret = VowpalWabbitInterface.GetFeatures(vw, example, ref feature_count);
int feature_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE));
- for (int i = 0; i < feature_count; i++)
+ for (int i = 0; i < (int)feature_count; i++)
{
IntPtr curfeaturePos = new IntPtr(ret.ToInt32() + i * feature_size);
f[i] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));
@@ -219,13 +219,13 @@ namespace cs_test
IntPtr.Zero == ex)
return;
- int featureSpaceLen = 0;
+ IntPtr featureSpaceLen = (IntPtr)0;
IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, ex, ref featureSpaceLen);
- this.featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
+ this.featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[(int)featureSpaceLen];
int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
- for (int i = 0; i < featureSpaceLen; i++)
+ for (int i = 0; i < (int)featureSpaceLen; i++)
{
IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
this.featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
@@ -239,7 +239,7 @@ namespace cs_test
}
}
- VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, featureSpaceLen);
+ VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, (IntPtr)featureSpaceLen);
}
}
private static void RunVWParse_and_VWLearn()
@@ -280,7 +280,7 @@ namespace cs_test
GCHandle pinnedFeatureSpace = GCHandle.Alloc(featureSpace, GCHandleType.Pinned);
IntPtr featureSpacePtr = pinnedFeatureSpace.AddrOfPinnedObject();
- IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, vwInstanceEx.featureSpace.Length);
+ IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, (IntPtr)vwInstanceEx.featureSpace.Length);
VowpalWabbitInterface.Learn(vw, importedExample);
VowpalWabbitInterface.FinishExample(vw, importedExample);
diff --git a/cs_test/VowpalWabbitInterface.cs b/cs_test/VowpalWabbitInterface.cs
index 2a1b412f..29382563 100644
--- a/cs_test/VowpalWabbitInterface.cs
+++ b/cs_test/VowpalWabbitInterface.cs
@@ -1,108 +1,127 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Runtime.InteropServices;
-
-namespace Microsoft.Research.MachineLearning
-{
- public sealed class VowpalWabbitInterface
- {
- [StructLayout(LayoutKind.Sequential)]
- public struct FEATURE_SPACE
- {
- public byte name;
- public IntPtr features; // points to a FEATURE[]
- public int len;
- }
-
- [StructLayout(LayoutKind.Sequential)]
- public struct FEATURE
- {
- public float x;
- public uint weight_index;
- }
-
- [DllImport("libvw.dll", EntryPoint = "VW_Initialize", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr Initialize([MarshalAs(UnmanagedType.LPWStr)]string arguments);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Finish", CallingConvention = CallingConvention.StdCall)]
- public static extern void Finish(IntPtr vw);
-
- [DllImport("libvw.dll", EntryPoint = "VW_ImportExample", CallingConvention = CallingConvention.StdCall)]
- // features points to a FEATURE_SPACE[]
- public static extern IntPtr ImportExample(IntPtr vw, IntPtr features, int length);
-
- [DllImport("libvw.dll", EntryPoint = "VW_ExportExample", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr ExportExample(IntPtr vw, IntPtr example, ref int length);
-
- [DllImport("libvw.dll", EntryPoint = "VW_ReleaseFeatureSpace", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr ReleaseFeatureSpace(IntPtr fs, int length);
-
- [DllImport("libvw.dll", EntryPoint = "VW_ReadExample", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr ReadExample(IntPtr vw, [MarshalAs(UnmanagedType.LPWStr)]string exampleString);
-
- [DllImport("libvw.dll", EntryPoint = "VW_StartParser", CallingConvention = CallingConvention.StdCall)]
- public static extern void StartParser(IntPtr vw, bool do_init);
-
- [DllImport("libvw.dll", EntryPoint = "VW_EndParser", CallingConvention = CallingConvention.StdCall)]
- public static extern void EndParser(IntPtr vw);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetExample", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr GetExample(IntPtr parser);
-
- [DllImport("libvw.dll", EntryPoint = "VW_FinishExample", CallingConvention = CallingConvention.StdCall)]
- public static extern void FinishExample(IntPtr vw, IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetLabel", CallingConvention = CallingConvention.StdCall)]
- public static extern float GetLabel(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetImportance", CallingConvention = CallingConvention.StdCall)]
- public static extern float GetImportance(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetInitial", CallingConvention = CallingConvention.StdCall)]
- public static extern float GetInitial(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetPrediction", CallingConvention = CallingConvention.StdCall)]
- public static extern float GetPrediction(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetTagLength", CallingConvention = CallingConvention.StdCall)]
- public static extern UInt32 GetTagLength(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetTag", CallingConvention = CallingConvention.StdCall)]
- public static extern byte GetTag(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetFeatureNumber", CallingConvention = CallingConvention.StdCall)]
- public static extern UInt32 GetFeatureNumber(IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_GetFeatures", CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr GetFeatures(IntPtr vw, IntPtr example, ref int length);
-
- [DllImport("libvw.dll", EntryPoint = "VW_ReturnFeatures", CallingConvention = CallingConvention.StdCall)]
- public static extern void ReturnFeatures(IntPtr features);
-
- [DllImport("libvw.dll", EntryPoint = "VW_HashSpace", CallingConvention = CallingConvention.StdCall)]
- public static extern uint HashSpace(IntPtr vw, [MarshalAs(UnmanagedType.LPWStr)]string s);
-
- [DllImport("libvw.dll", EntryPoint = "VW_HashFeature", CallingConvention = CallingConvention.StdCall)]
- public static extern uint HashFeature(IntPtr vw, [MarshalAs(UnmanagedType.LPWStr)]string s, ulong u);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Learn", CallingConvention = CallingConvention.StdCall)]
- public static extern float Learn(IntPtr vw, IntPtr example);
-
- [DllImport("libvw.dll", EntryPoint = "VW_AddLabel", CallingConvention = CallingConvention.StdCall)]
- public static extern void AddLabel(IntPtr example, float label=float.MaxValue, float weight=1, float initial=0);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Get_Weight", CallingConvention = CallingConvention.StdCall)]
- public static extern float Get_Weight(IntPtr vw, UInt32 index, UInt32 offset);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Set_Weight", CallingConvention = CallingConvention.StdCall)]
- public static extern void Set_Weight(IntPtr vw, UInt32 index, UInt32 offset, float value);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Num_Weights", CallingConvention = CallingConvention.StdCall)]
- public static extern UInt32 Num_Weights(IntPtr vw);
-
- [DllImport("libvw.dll", EntryPoint = "VW_Get_Stride", CallingConvention = CallingConvention.StdCall)]
- public static extern UInt32 Get_Stride(IntPtr vw);
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace Microsoft.Research.MachineLearning
+{
+ using SizeT = IntPtr;
+ using VwHandle = IntPtr;
+ using VwFeatureSpace = IntPtr;
+ using VwExample = IntPtr;
+ using VwFeature = IntPtr;
+ using BytePtr = IntPtr;
+
+ public sealed class VowpalWabbitInterface
+ {
+ private const string LIBVW = "libvw.dll";
+
+ [StructLayout(LayoutKind.Sequential)]
+ public struct FEATURE_SPACE
+ {
+ public byte name;
+ public IntPtr features; // points to a FEATURE[]
+ public int len;
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ public struct FEATURE
+ {
+ public float x;
+ public uint weight_index;
+ }
+
+ [DllImport(LIBVW, EntryPoint = "VW_Initialize")]
+ public static extern VwHandle Initialize([MarshalAs(UnmanagedType.LPWStr)]string arguments);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Finish")]
+ public static extern void Finish(VwHandle vw);
+
+ [DllImport(LIBVW, EntryPoint = "VW_ImportExample")]
+ // features points to a FEATURE_SPACE[]
+ public static extern VwExample ImportExample(VwHandle vw, VwFeatureSpace features, SizeT length);
+
+ [DllImport(LIBVW, EntryPoint = "VW_ExportExample")]
+ public static extern VwFeatureSpace ExportExample(VwHandle vw, VwExample example, ref SizeT length);
+
+ [DllImport(LIBVW, EntryPoint = "VW_ReleaseFeatureSpace")]
+ public static extern void ReleaseFeatureSpace(VwFeatureSpace fs, SizeT length);
+
+ [DllImport(LIBVW, EntryPoint = "VW_ReadExample")]
+ public static extern VwExample ReadExample(VwHandle vw, [MarshalAs(UnmanagedType.LPWStr)]string exampleString);
+
+ // Have to marshal bools, C++ considers them 4 byte quantities, and C# considers them 1 byte.
+ [DllImport(LIBVW, EntryPoint = "VW_StartParser")]
+ public static extern void StartParser(VwHandle vw, [MarshalAs(UnmanagedType.Bool)]bool do_init);
+
+ [DllImport(LIBVW, EntryPoint = "VW_EndParser")]
+ public static extern void EndParser(VwHandle vw);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetExample")]
+ public static extern VwExample GetExample(VwHandle parser);
+
+ [DllImport(LIBVW, EntryPoint = "VW_FinishExample")]
+ public static extern void FinishExample(VwHandle vw, VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetLabel")]
+ public static extern float GetLabel(VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetImportance")]
+ public static extern float GetImportance(VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetInitial")]
+ public static extern float GetInitial(VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetPrediction")]
+ public static extern float GetPrediction(VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetTagLength")]
+ public static extern SizeT GetTagLength(VwExample example);
+
+ // Saying this returned a byte was inappropriate, because you were returning
+ // actually a pointer to a seqeunce of bytes. (Not sure what the interpretation
+ // of this should be, utf8 or something?)
+ [DllImport(LIBVW, EntryPoint = "VW_GetTag")]
+ public static extern BytePtr GetTag(VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_GetFeatureNumber")]
+ public static extern SizeT GetFeatureNumber(VwExample example);
+
+ // Same note regarding ref int vs size_t*
+ [DllImport(LIBVW, EntryPoint = "VW_GetFeatures")]
+ public static extern VwFeature GetFeatures(VwHandle vw, VwExample example, ref SizeT length);
+
+ [DllImport(LIBVW, EntryPoint = "VW_ReturnFeatures")]
+ public static extern void ReturnFeatures(VwExample features);
+
+ [DllImport(LIBVW, EntryPoint = "VW_HashSpace")]
+ public static extern uint HashSpace(VwHandle vw, [MarshalAs(UnmanagedType.LPWStr)]string s);
+
+ // The DLL defines the last argument "u" as being an "unsigned long".
+ // In C++ under current circumstances, both ints and longs are four byte integers.
+ // If you wanted an eight byte integer you should use "long long" (or probably
+ // more appropriately in this circumstance size_t).
+ // In C#, "int" is four bytes, "long" is eight bytes.
+ [DllImport(LIBVW, EntryPoint = "VW_HashFeature")]
+ public static extern uint HashFeature(VwHandle vw, [MarshalAs(UnmanagedType.LPWStr)]string s, uint u);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Learn")]
+ public static extern float Learn(VwHandle vw, VwExample example);
+
+ [DllImport(LIBVW, EntryPoint = "VW_AddLabel")]
+ public static extern void AddLabel(VwExample example, float label = float.MaxValue, float weight = 1, float initial = 0);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Get_Weight")]
+ public static extern float Get_Weight(VwHandle vw, SizeT index, SizeT offset);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Set_Weight")]
+ public static extern void Set_Weight(VwHandle vw, SizeT index, SizeT offset, float value);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Num_Weights")]
+ public static extern SizeT Num_Weights(VwHandle vw);
+
+ [DllImport(LIBVW, EntryPoint = "VW_Get_Stride")]
+ public static extern SizeT Get_Stride(VwHandle vw);
+ }
+}