You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resource_specification.go 18 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "fmt"
  5. "strings"
  6. "xorm.io/builder"
  7. )
  8. const (
  9. SpecNotVerified int = iota + 1
  10. SpecOnShelf
  11. SpecOffShelf
  12. )
  13. type SearchSpecOrderBy int
  14. const (
  15. SearchSpecOrderById SearchSpecOrderBy = iota
  16. SearchSpecOrder4Standard
  17. )
  18. type ResourceSpecification struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. QueueId int64 `xorm:"INDEX"`
  21. SourceSpecId string `xorm:"INDEX"`
  22. AccCardsNum int
  23. CpuCores int
  24. MemGiB float32
  25. GPUMemGiB float32
  26. ShareMemGiB float32
  27. UnitPrice int
  28. Status int
  29. IsAvailable bool
  30. IsAutomaticSync bool
  31. CreatedTime timeutil.TimeStamp `xorm:"created"`
  32. CreatedBy int64
  33. UpdatedTime timeutil.TimeStamp `xorm:"updated"`
  34. UpdatedBy int64
  35. }
  36. func (r ResourceSpecification) ConvertToRes() *ResourceSpecificationRes {
  37. return &ResourceSpecificationRes{
  38. ID: r.ID,
  39. SourceSpecId: r.SourceSpecId,
  40. AccCardsNum: r.AccCardsNum,
  41. CpuCores: r.CpuCores,
  42. MemGiB: r.MemGiB,
  43. ShareMemGiB: r.ShareMemGiB,
  44. GPUMemGiB: r.GPUMemGiB,
  45. UnitPrice: r.UnitPrice,
  46. Status: r.Status,
  47. IsAvailable: r.IsAvailable,
  48. UpdatedTime: r.UpdatedTime,
  49. }
  50. }
  51. type ResourceSpecificationReq struct {
  52. QueueId int64 `binding:"Required"`
  53. SourceSpecId string
  54. AccCardsNum int
  55. CpuCores int
  56. MemGiB float32
  57. GPUMemGiB float32
  58. ShareMemGiB float32
  59. UnitPrice int
  60. Status int
  61. IsAutomaticSync bool
  62. CreatorId int64
  63. }
  64. func (r ResourceSpecificationReq) ToDTO() ResourceSpecification {
  65. return ResourceSpecification{
  66. QueueId: r.QueueId,
  67. SourceSpecId: r.SourceSpecId,
  68. AccCardsNum: r.AccCardsNum,
  69. CpuCores: r.CpuCores,
  70. MemGiB: r.MemGiB,
  71. GPUMemGiB: r.GPUMemGiB,
  72. ShareMemGiB: r.ShareMemGiB,
  73. UnitPrice: r.UnitPrice,
  74. Status: r.Status,
  75. IsAutomaticSync: r.IsAutomaticSync,
  76. CreatedBy: r.CreatorId,
  77. UpdatedBy: r.CreatorId,
  78. IsAvailable: true,
  79. }
  80. }
  81. type SearchResourceSpecificationOptions struct {
  82. ListOptions
  83. QueueId int64
  84. Status int
  85. Cluster string
  86. AvailableCode int
  87. OrderBy SearchSpecOrderBy
  88. }
  89. type SearchResourceBriefSpecificationOptions struct {
  90. QueueId int64
  91. Cluster string
  92. }
  93. type ResourceSpecAndQueueListRes struct {
  94. TotalSize int64
  95. List []*ResourceSpecAndQueueRes
  96. }
  97. func NewResourceSpecAndQueueListRes(totalSize int64, list []ResourceSpecAndQueue) *ResourceSpecAndQueueListRes {
  98. resList := make([]*ResourceSpecAndQueueRes, len(list))
  99. for i, v := range list {
  100. resList[i] = v.ConvertToRes()
  101. }
  102. return &ResourceSpecAndQueueListRes{
  103. TotalSize: totalSize,
  104. List: resList,
  105. }
  106. }
  107. type ResourceSpecificationRes struct {
  108. ID int64
  109. SourceSpecId string
  110. AccCardsNum int
  111. CpuCores int
  112. MemGiB float32
  113. GPUMemGiB float32
  114. ShareMemGiB float32
  115. UnitPrice int
  116. Status int
  117. IsAvailable bool
  118. UpdatedTime timeutil.TimeStamp
  119. }
  120. func (ResourceSpecificationRes) TableName() string {
  121. return "resource_specification"
  122. }
  123. type ResourceSpecAndQueueRes struct {
  124. Spec *ResourceSpecificationRes
  125. Queue *ResourceQueueRes
  126. }
  127. type ResourceSpecAndQueue struct {
  128. ResourceSpecification `xorm:"extends"`
  129. ResourceQueue `xorm:"extends"`
  130. }
  131. func (*ResourceSpecAndQueue) TableName() string {
  132. return "resource_specification"
  133. }
  134. func (r ResourceSpecAndQueue) ConvertToRes() *ResourceSpecAndQueueRes {
  135. return &ResourceSpecAndQueueRes{
  136. Spec: r.ResourceSpecification.ConvertToRes(),
  137. Queue: r.ResourceQueue.ConvertToRes(),
  138. }
  139. }
  140. type FindSpecsOptions struct {
  141. JobType JobType
  142. ComputeResource string
  143. Cluster string
  144. AiCenterCode string
  145. SpecId int64
  146. QueueCode string
  147. SourceSpecId string
  148. AccCardsNum int
  149. UseAccCardsNum bool
  150. AccCardType string
  151. CpuCores int
  152. UseCpuCores bool
  153. MemGiB float32
  154. UseMemGiB bool
  155. GPUMemGiB float32
  156. UseGPUMemGiB bool
  157. ShareMemGiB float32
  158. UseShareMemGiB bool
  159. //if true,find specs no matter used or not used in scene. if false,only find specs used in scene
  160. RequestAll bool
  161. SpecStatus int
  162. }
  163. type Specification struct {
  164. ID int64
  165. SourceSpecId string
  166. AccCardsNum int
  167. AccCardType string
  168. CpuCores int
  169. MemGiB float32
  170. GPUMemGiB float32
  171. ShareMemGiB float32
  172. ComputeResource string
  173. UnitPrice int
  174. QueueId int64
  175. QueueCode string
  176. Cluster string
  177. AiCenterCode string
  178. AiCenterName string
  179. IsExclusive bool
  180. ExclusiveOrg string
  181. //specs that have the same sourceSpecId, computeResource and cluster as current spec
  182. RelatedSpecs []*Specification
  183. }
  184. func (Specification) TableName() string {
  185. return "resource_specification"
  186. }
  187. func (s *Specification) loadRelatedSpecs() {
  188. if s.RelatedSpecs != nil {
  189. return
  190. }
  191. defaultSpecs := make([]*Specification, 0)
  192. if s.SourceSpecId == "" {
  193. s.RelatedSpecs = defaultSpecs
  194. return
  195. }
  196. r, err := FindSpecs(FindSpecsOptions{
  197. ComputeResource: s.ComputeResource,
  198. Cluster: s.Cluster,
  199. SourceSpecId: s.SourceSpecId,
  200. RequestAll: true,
  201. SpecStatus: SpecOnShelf,
  202. })
  203. if err != nil {
  204. s.RelatedSpecs = defaultSpecs
  205. return
  206. }
  207. s.RelatedSpecs = r
  208. }
  209. func (s *Specification) GetAvailableCenterIds(userIds ...int64) []string {
  210. s.loadRelatedSpecs()
  211. if len(s.RelatedSpecs) == 0 {
  212. return make([]string, 0)
  213. }
  214. var uId int64
  215. if len(userIds) > 0 {
  216. uId = userIds[0]
  217. }
  218. //filter exclusive specs
  219. specs := FilterExclusiveSpecs(s.RelatedSpecs, uId)
  220. centerIds := make([]string, len(specs))
  221. for i, v := range specs {
  222. centerIds[i] = v.AiCenterCode
  223. }
  224. return centerIds
  225. }
  226. func FilterExclusiveSpecs(r []*Specification, userId int64) []*Specification {
  227. if userId == 0 {
  228. return r
  229. }
  230. specs := make([]*Specification, 0, len(r))
  231. specMap := make(map[int64]string, 0)
  232. for i := 0; i < len(r); i++ {
  233. spec := r[i]
  234. if _, has := specMap[spec.ID]; has {
  235. continue
  236. }
  237. if !spec.IsExclusive {
  238. specs = append(specs, spec)
  239. specMap[spec.ID] = ""
  240. continue
  241. }
  242. orgs := strings.Split(spec.ExclusiveOrg, ";")
  243. for _, org := range orgs {
  244. isMember, _ := IsOrganizationMemberByOrgName(org, userId)
  245. if isMember {
  246. specs = append(specs, spec)
  247. specMap[spec.ID] = ""
  248. break
  249. }
  250. }
  251. }
  252. return specs
  253. }
  254. func DistinctSpecs(r []*Specification) []*Specification {
  255. specs := make([]*Specification, 0, len(r))
  256. sourceSpecIdMap := make(map[string]string, 0)
  257. for i := 0; i < len(r); i++ {
  258. spec := r[i]
  259. if spec.SourceSpecId == "" {
  260. specs = append(specs, spec)
  261. continue
  262. }
  263. if _, has := sourceSpecIdMap[spec.SourceSpecId]; has {
  264. continue
  265. }
  266. specs = append(specs, spec)
  267. sourceSpecIdMap[spec.SourceSpecId] = ""
  268. }
  269. return specs
  270. }
  271. func InsertResourceSpecification(r ResourceSpecification) (int64, error) {
  272. return x.Insert(&r)
  273. }
  274. func UpdateResourceSpecificationById(queueId int64, spec ResourceSpecification) (int64, error) {
  275. return x.ID(queueId).Update(&spec)
  276. }
  277. func UpdateSpecUnitPriceById(id int64, unitPrice int) error {
  278. _, err := x.Exec("update resource_specification set unit_price = ? ,updated_time = ? where id = ?", unitPrice, timeutil.TimeStampNow(), id)
  279. return err
  280. }
  281. func SearchResourceSpecification(opts SearchResourceSpecificationOptions) (int64, []ResourceSpecAndQueue, error) {
  282. var cond = builder.NewCond()
  283. if opts.Page <= 0 {
  284. opts.Page = 1
  285. }
  286. if opts.QueueId > 0 {
  287. cond = cond.And(builder.Eq{"resource_specification.queue_id": opts.QueueId})
  288. }
  289. if opts.Status > 0 {
  290. cond = cond.And(builder.Eq{"resource_specification.status": opts.Status})
  291. }
  292. if opts.Cluster != "" {
  293. cond = cond.And(builder.Eq{"resource_queue.cluster": opts.Cluster})
  294. }
  295. if opts.AvailableCode == 1 {
  296. cond = cond.And(builder.Eq{"resource_specification.is_available": true})
  297. } else if opts.AvailableCode == 2 {
  298. cond = cond.And(builder.Eq{"resource_specification.is_available": false})
  299. }
  300. //cond = cond.And(builder.Or(builder.Eq{"resource_queue.deleted_time": 0}).Or(builder.IsNull{"resource_queue.deleted_time"}))
  301. n, err := x.Where(cond).Join("INNER", "resource_queue", "resource_queue.ID = resource_specification.queue_id").
  302. Unscoped().Count(&ResourceSpecAndQueue{})
  303. if err != nil {
  304. return 0, nil, err
  305. }
  306. var orderby = ""
  307. switch opts.OrderBy {
  308. case SearchSpecOrder4Standard:
  309. orderby = "resource_queue.compute_resource asc,resource_queue.acc_card_type asc,resource_specification.acc_cards_num asc,resource_specification.cpu_cores asc,resource_specification.mem_gi_b asc,resource_specification.share_mem_gi_b asc"
  310. default:
  311. orderby = "resource_specification.id desc"
  312. }
  313. r := make([]ResourceSpecAndQueue, 0)
  314. err = x.Where(cond).
  315. Join("INNER", "resource_queue", "resource_queue.ID = resource_specification.queue_id").
  316. OrderBy(orderby).
  317. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  318. Unscoped().Find(&r)
  319. if err != nil {
  320. return 0, nil, err
  321. }
  322. return n, r, nil
  323. }
  324. func GetSpecScenes(specId int64) ([]ResourceSceneBriefRes, error) {
  325. r := make([]ResourceSceneBriefRes, 0)
  326. err := x.Where("resource_scene_spec.spec_id = ?", specId).
  327. Join("INNER", "resource_scene_spec", "resource_scene_spec.scene_id = resource_scene.id").
  328. Find(&r)
  329. if err != nil {
  330. return nil, err
  331. }
  332. return r, nil
  333. }
  334. func ResourceSpecOnShelf(id int64, unitPrice int) error {
  335. _, err := x.Exec("update resource_specification set unit_price = ?,updated_time = ?,status = ? where id = ?", unitPrice, timeutil.TimeStampNow(), SpecOnShelf, id)
  336. return err
  337. }
  338. func ResourceSpecOffShelf(id int64) (int64, error) {
  339. sess := x.NewSession()
  340. var err error
  341. defer func() {
  342. if err != nil {
  343. sess.Rollback()
  344. }
  345. sess.Close()
  346. }()
  347. param := ResourceSpecification{
  348. Status: SpecOffShelf,
  349. }
  350. n, err := sess.Where("id = ? and status = ?", id, SpecOnShelf).Update(&param)
  351. if err != nil {
  352. return 0, err
  353. }
  354. sess.Commit()
  355. return n, err
  356. }
  357. func GetResourceSpecificationByIds(ids []int64) ([]*Specification, error) {
  358. r := make([]*Specification, 0)
  359. err := x.In("resource_specification.id", ids).
  360. Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id").
  361. Find(&r)
  362. return r, err
  363. }
  364. func GetResourceSpecification(r *ResourceSpecification) (*ResourceSpecification, error) {
  365. has, err := x.Get(r)
  366. if err != nil {
  367. return nil, err
  368. } else if !has {
  369. return nil, nil
  370. }
  371. return r, nil
  372. }
  373. func SyncGrampusSpecs(updateList []ResourceSpecification, insertList []ResourceSpecification, existIds []int64) error {
  374. sess := x.NewSession()
  375. var err error
  376. defer func() {
  377. if err != nil {
  378. sess.Rollback()
  379. }
  380. sess.Close()
  381. }()
  382. //delete specs and scene that no longer exists
  383. deleteIds := make([]int64, 0)
  384. cond := builder.NewCond()
  385. cond = cond.And(builder.NotIn("resource_specification.id", existIds)).And(builder.Eq{"resource_queue.cluster": C2NetCluster})
  386. if err := sess.Cols("resource_specification.id").Table("resource_specification").
  387. Where(cond).Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id").
  388. Find(&deleteIds); err != nil {
  389. return err
  390. }
  391. if len(deleteIds) > 0 {
  392. if _, err = sess.Cols("status", "is_available").In("id", deleteIds).Update(&ResourceSpecification{Status: SpecOffShelf, IsAvailable: false}); err != nil {
  393. return err
  394. }
  395. }
  396. //update exists specs
  397. if len(updateList) > 0 {
  398. for _, v := range updateList {
  399. if _, err = sess.ID(v.ID).UseBool("is_available").Update(&v); err != nil {
  400. return err
  401. }
  402. }
  403. }
  404. //insert new specs
  405. if len(insertList) > 0 {
  406. if _, err = sess.Insert(insertList); err != nil {
  407. return err
  408. }
  409. }
  410. return sess.Commit()
  411. }
  412. //FindSpecs
  413. func FindSpecs(opts FindSpecsOptions) ([]*Specification, error) {
  414. var cond = builder.NewCond()
  415. if !opts.RequestAll && opts.JobType != "" {
  416. cond = cond.And(builder.Eq{"resource_scene.job_type": opts.JobType})
  417. }
  418. if opts.ComputeResource != "" {
  419. cond = cond.And(builder.Eq{"resource_queue.compute_resource": opts.ComputeResource})
  420. }
  421. if opts.Cluster != "" {
  422. cond = cond.And(builder.Eq{"resource_queue.cluster": opts.Cluster})
  423. }
  424. if opts.AiCenterCode != "" {
  425. cond = cond.And(builder.Eq{"resource_queue.ai_center_code": opts.AiCenterCode})
  426. }
  427. if opts.SpecId > 0 {
  428. cond = cond.And(builder.Eq{"resource_specification.id": opts.SpecId})
  429. }
  430. if opts.QueueCode != "" {
  431. cond = cond.And(builder.Eq{"resource_queue.queue_code": opts.QueueCode})
  432. }
  433. if opts.SourceSpecId != "" {
  434. cond = cond.And(builder.Eq{"resource_specification.source_spec_id": opts.SourceSpecId})
  435. }
  436. if opts.UseAccCardsNum {
  437. cond = cond.And(builder.Eq{"resource_specification.acc_cards_num": opts.AccCardsNum})
  438. }
  439. if opts.AccCardType != "" {
  440. cond = cond.And(builder.Eq{"resource_queue.acc_card_type": opts.AccCardType})
  441. }
  442. if opts.UseCpuCores {
  443. cond = cond.And(builder.Eq{"resource_specification.cpu_cores": opts.CpuCores})
  444. }
  445. if opts.UseMemGiB {
  446. cond = cond.And(builder.Eq{"resource_specification.mem_gi_b": opts.MemGiB})
  447. }
  448. if opts.UseGPUMemGiB {
  449. cond = cond.And(builder.Eq{"resource_specification.gpu_mem_gi_b": opts.GPUMemGiB})
  450. }
  451. if opts.UseShareMemGiB {
  452. cond = cond.And(builder.Eq{"resource_specification.share_mem_gi_b": opts.ShareMemGiB})
  453. }
  454. if opts.SpecStatus > 0 {
  455. cond = cond.And(builder.Eq{"resource_specification.status": opts.SpecStatus})
  456. }
  457. r := make([]*Specification, 0)
  458. s := x.Where(cond).
  459. Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id")
  460. if !opts.RequestAll {
  461. s = s.Join("INNER", "resource_scene_spec", "resource_scene_spec.spec_id = resource_specification.id").
  462. Join("INNER", "resource_scene", "resource_scene_spec.scene_id = resource_scene.id")
  463. }
  464. err := s.OrderBy("resource_queue.compute_resource asc,resource_queue.acc_card_type asc,resource_specification.acc_cards_num asc,resource_specification.cpu_cores asc,resource_specification.mem_gi_b asc,resource_specification.share_mem_gi_b asc").
  465. Unscoped().Find(&r)
  466. if err != nil {
  467. return nil, err
  468. }
  469. return r, nil
  470. }
  471. func InitQueueAndSpec(queue ResourceQueue, spec ResourceSpecification) (*Specification, error) {
  472. sess := x.NewSession()
  473. defer sess.Close()
  474. sess.Begin()
  475. param := ResourceQueue{
  476. QueueCode: queue.QueueCode,
  477. Cluster: queue.Cluster,
  478. AiCenterCode: queue.AiCenterCode,
  479. ComputeResource: queue.ComputeResource,
  480. AccCardType: queue.AccCardType,
  481. }
  482. _, err := sess.Get(&param)
  483. if err != nil {
  484. sess.Rollback()
  485. return nil, err
  486. }
  487. if param.ID == 0 {
  488. _, err = sess.InsertOne(&queue)
  489. if err != nil {
  490. sess.Rollback()
  491. return nil, err
  492. }
  493. } else {
  494. queue = param
  495. }
  496. spec.QueueId = queue.ID
  497. _, err = sess.InsertOne(&spec)
  498. if err != nil {
  499. sess.Rollback()
  500. return nil, err
  501. }
  502. sess.Commit()
  503. return BuildSpecification(queue, spec), nil
  504. }
  505. func BuildSpecification(queue ResourceQueue, spec ResourceSpecification) *Specification {
  506. return &Specification{
  507. ID: spec.ID,
  508. SourceSpecId: spec.SourceSpecId,
  509. AccCardsNum: spec.AccCardsNum,
  510. AccCardType: queue.AccCardType,
  511. CpuCores: spec.CpuCores,
  512. MemGiB: spec.MemGiB,
  513. GPUMemGiB: spec.GPUMemGiB,
  514. ShareMemGiB: spec.ShareMemGiB,
  515. ComputeResource: queue.ComputeResource,
  516. UnitPrice: spec.UnitPrice,
  517. QueueId: queue.ID,
  518. QueueCode: queue.QueueCode,
  519. Cluster: queue.Cluster,
  520. AiCenterCode: queue.AiCenterCode,
  521. AiCenterName: queue.AiCenterName,
  522. }
  523. }
  524. func GetCloudbrainOneAccCardType(queueCode string) string {
  525. switch queueCode {
  526. case "a100":
  527. return "A100"
  528. case "openidebug":
  529. return "T4"
  530. case "openidgx":
  531. return "V100"
  532. }
  533. return ""
  534. }
  535. var cloudbrainTwoSpecsInitFlag = false
  536. var cloudbrainTwoSpecs map[string]*Specification
  537. func GetCloudbrainTwoSpecs() (map[string]*Specification, error) {
  538. if !cloudbrainTwoSpecsInitFlag {
  539. r, err := InitCloudbrainTwoSpecs()
  540. if err != nil {
  541. return nil, err
  542. }
  543. cloudbrainTwoSpecsInitFlag = true
  544. cloudbrainTwoSpecs = r
  545. }
  546. return cloudbrainTwoSpecs, nil
  547. }
  548. func InitCloudbrainTwoSpecs() (map[string]*Specification, error) {
  549. r := make(map[string]*Specification, 0)
  550. queue, err := GetResourceQueue(&ResourceQueue{QueueCode: "openisupport"})
  551. if err != nil {
  552. return nil, err
  553. }
  554. if queue == nil {
  555. queue = &ResourceQueue{
  556. QueueCode: "openisupport",
  557. Cluster: OpenICluster,
  558. AiCenterCode: AICenterOfCloudBrainTwo,
  559. AiCenterName: "云脑二",
  560. ComputeResource: NPU,
  561. AccCardType: "ASCEND910",
  562. Remark: "处理历史云脑任务时自动生成",
  563. }
  564. _, err = x.InsertOne(queue)
  565. if err != nil {
  566. return nil, err
  567. }
  568. }
  569. for i := 1; i <= 8; i = i * 2 {
  570. sourceSpecId := "modelarts.bm.910.arm.public." + fmt.Sprint(i)
  571. spec, err := GetResourceSpecification(&ResourceSpecification{
  572. SourceSpecId: sourceSpecId,
  573. QueueId: queue.ID,
  574. })
  575. if err != nil {
  576. return nil, err
  577. }
  578. if spec == nil {
  579. spec = &ResourceSpecification{
  580. QueueId: queue.ID,
  581. SourceSpecId: sourceSpecId,
  582. AccCardsNum: i,
  583. CpuCores: i * 24,
  584. MemGiB: float32(i * 256),
  585. GPUMemGiB: float32(32),
  586. Status: SpecOffShelf,
  587. IsAvailable: true,
  588. }
  589. _, err = x.Insert(spec)
  590. if err != nil {
  591. return nil, err
  592. }
  593. }
  594. r[sourceSpecId] = BuildSpecification(*queue, *spec)
  595. }
  596. return r, nil
  597. }
  598. var grampusSpecsInitFlag = false
  599. var grampusSpecs map[string]*Specification
  600. func GetGrampusSpecs() (map[string]*Specification, error) {
  601. if !grampusSpecsInitFlag {
  602. specMap := make(map[string]*Specification, 0)
  603. r, err := FindSpecs(FindSpecsOptions{
  604. Cluster: C2NetCluster,
  605. RequestAll: true,
  606. })
  607. if err != nil {
  608. return nil, err
  609. }
  610. for _, spec := range r {
  611. specMap[spec.SourceSpecId] = spec
  612. specMap[spec.SourceSpecId+"_"+spec.AiCenterCode] = spec
  613. }
  614. grampusSpecsInitFlag = true
  615. grampusSpecs = specMap
  616. }
  617. return grampusSpecs, nil
  618. }