opencl harmonytask another try
This commit is contained in:
parent
82d0c2889b
commit
eb294c9714
15
lib/harmony/resources/miniopencl/cl.h
Normal file
15
lib/harmony/resources/miniopencl/cl.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#ifndef CL_H
|
||||||
|
#define CL_H
|
||||||
|
|
||||||
|
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
|
||||||
|
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
|
||||||
|
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include "OpenCL/opencl.h"
|
||||||
|
#else
|
||||||
|
#include "CL/opencl.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* CL_H */
|
87
lib/harmony/resources/miniopencl/miniopencl.go
Normal file
87
lib/harmony/resources/miniopencl/miniopencl.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package cl
|
||||||
|
|
||||||
|
// #include "cl.h"
|
||||||
|
import "C"
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxPlatforms = 32
|
||||||
|
|
||||||
|
type Platform struct {
|
||||||
|
id C.cl_platform_id
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtain the list of platforms available.
|
||||||
|
func GetPlatforms() ([]*Platform, error) {
|
||||||
|
var platformIds [maxPlatforms]C.cl_platform_id
|
||||||
|
var nPlatforms C.cl_uint
|
||||||
|
if err := C.clGetPlatformIDs(C.cl_uint(maxPlatforms), &platformIds[0], &nPlatforms); err != C.CL_SUCCESS {
|
||||||
|
return nil, toError(err)
|
||||||
|
}
|
||||||
|
platforms := make([]*Platform, nPlatforms)
|
||||||
|
for i := 0; i < int(nPlatforms); i++ {
|
||||||
|
platforms[i] = &Platform{id: platformIds[i]}
|
||||||
|
}
|
||||||
|
return platforms, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxDeviceCount = 64
|
||||||
|
|
||||||
|
type DeviceType uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
DeviceTypeAll DeviceType = C.CL_DEVICE_TYPE_ALL
|
||||||
|
)
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
id C.cl_device_id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) GetAllDevices() ([]*Device, error) {
|
||||||
|
var deviceIds [maxDeviceCount]C.cl_device_id
|
||||||
|
var numDevices C.cl_uint
|
||||||
|
var platformId C.cl_platform_id
|
||||||
|
if p != nil {
|
||||||
|
platformId = p.id
|
||||||
|
}
|
||||||
|
if err := C.clGetDeviceIDs(platformId, C.cl_device_type(DeviceTypeAll), C.cl_uint(maxDeviceCount), &deviceIds[0], &numDevices); err != C.CL_SUCCESS {
|
||||||
|
return nil, toError(err)
|
||||||
|
}
|
||||||
|
if numDevices > maxDeviceCount {
|
||||||
|
numDevices = maxDeviceCount
|
||||||
|
}
|
||||||
|
devices := make([]*Device, numDevices)
|
||||||
|
for i := 0; i < int(numDevices); i++ {
|
||||||
|
devices[i] = &Device{id: deviceIds[i]}
|
||||||
|
}
|
||||||
|
return devices, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toError(code C.cl_int) error {
|
||||||
|
return ErrOther(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrOther int
|
||||||
|
|
||||||
|
func (e ErrOther) Error() string {
|
||||||
|
return fmt.Sprintf("cl: error %d", int(e))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size of global device memory in bytes.
|
||||||
|
func (d *Device) GlobalMemSize() int64 {
|
||||||
|
val, _ := d.getInfoUlong(C.CL_DEVICE_GLOBAL_MEM_SIZE, true)
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Device) getInfoUlong(param C.cl_device_info, panicOnError bool) (int64, error) {
|
||||||
|
var val C.cl_ulong
|
||||||
|
if err := C.clGetDeviceInfo(d.id, param, C.size_t(unsafe.Sizeof(val)), unsafe.Pointer(&val), nil); err != C.CL_SUCCESS {
|
||||||
|
if panicOnError {
|
||||||
|
panic("Should never fail")
|
||||||
|
}
|
||||||
|
return 0, toError(err)
|
||||||
|
}
|
||||||
|
return int64(val), nil
|
||||||
|
}
|
@ -14,12 +14,12 @@ import (
|
|||||||
logging "github.com/ipfs/go-log/v2"
|
logging "github.com/ipfs/go-log/v2"
|
||||||
"github.com/pbnjay/memory"
|
"github.com/pbnjay/memory"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"github.com/samuel/go-opencl/cl"
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
ffi "github.com/filecoin-project/filecoin-ffi"
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
||||||
|
cl "github.com/filecoin-project/lotus/lib/harmony/resources/miniopencl"
|
||||||
)
|
)
|
||||||
|
|
||||||
var LOOKS_DEAD_TIMEOUT = 10 * time.Minute // Time w/o minute heartbeats
|
var LOOKS_DEAD_TIMEOUT = 10 * time.Minute // Time w/o minute heartbeats
|
||||||
@ -146,7 +146,7 @@ func getGpuRam() uint64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return uint64(lo.SumBy(platforms, func(p *cl.Platform) int64 {
|
return uint64(lo.SumBy(platforms, func(p *cl.Platform) int64 {
|
||||||
d, err := p.GetDevices(cl.DeviceTypeAll)
|
d, err := p.GetAllDevices()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return 0
|
return 0
|
||||||
|
Loading…
Reference in New Issue
Block a user